[英]Can Python multiprocessing process be launched from OTHER than __main__?
I am writing a framework in Python 3.8 on Debian that will launch some multiprocessing processes.我正在 Debian 上的 Python 3.8 中编写一个框架,它将启动一些多处理进程。 I want the configuration and launching of the processes to be done in functions OTHER than the main .我希望在main以外的函数中完成进程的配置和启动。 The main file will be written by the end user of the framework and they should not need to know about these processes.主文件将由框架的最终用户编写,他们不需要了解这些过程。 Hence I tried to put the code that configures and launches the processes in helper functions or class methods that the main will call.因此,我尝试将配置和启动进程的代码放在辅助函数或主调用的 class 方法中。
What I'm finding is as soon as the launcher function / method exits the processes die.我发现一旦启动器 function / 方法退出进程就会死掉。 This is even though the launcher functions / methods run (I think) in the same process as main which is still running.即使启动器功能/方法在与仍在运行的main相同的进程中运行(我认为)也是如此。 I have put a long time.sleep in the launcher functions / methods right before they exit and it seems the processes are alive for that long.在启动器功能/方法退出之前,我已经在启动器功能/方法中放置了很长时间,而且这些进程似乎已经存在了很长时间。
I tried setting the 'daemon' flag but that doesn't seem to solve it.我尝试设置“守护进程”标志,但这似乎并没有解决它。 If this is truly a limitation of multiprocessing I can instruct the users of my framework to always put some boiler-plate launcher code in their file, but it seems clunky.如果这确实是多处理的限制,我可以指示我的框架的用户始终将一些样板启动器代码放在他们的文件中,但这似乎很笨重。 All help is appreciated!感谢所有帮助!
The function that launches the processes was returning only the processes to main and NOT managed queues that the processes were using.启动进程的 function 仅将进程返回到进程正在使用的主队列和非托管队列。 I changed to return a dict with the processes AND the managed queues and everything works.我更改为返回带有进程和托管队列的字典,一切正常。 This is even though main does NOT use the queues.即使main不使用队列也是如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.