简体   繁体   English

Python 多处理进程是否可以从 __main__ 以外的其他地方启动?

[英]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.

相关问题 主模块以外的模块中的Python多处理启动过程 - Python Multiprocessing Start Process in Module Other Than Main Windows 上的 python 多处理,如果 __name__ == “__main__” - python multiprocessing on windows, if __name__ == “__main__” Python 星图多处理和 __main__ 问题 - Python multiprocessing with starmap and issue with __main__ 在Python multiprocessing.Process中,我们是否必须使用`__name__ == __main__`? - In Python multiprocessing.Process , do we have to use `__name__ == __main__`? 在Windows上启动新进程时,为什么Python的多处理模块会导入__main__? - Why does Python's multiprocessing module import __main__ when starting a new process on Windows? Python 多处理错误:AttributeError: Can't get attribute 'task' on<module '__main__' (built-in)> "</module> - Python Multiprocessing error: AttributeError: Can't get attribute 'task' on <module '__main__' (built-in)>" 在Python多处理中使用__name __ ==&#39;__ main__&#39;的解决方法 - Workaround for using __name__=='__main__' in Python multiprocessing 使用python-multiprocessing与if __name__ ==&#39;__main__&#39;相关的谜 - enigma using python-multiprocessing related with if __name__ == '__main__' 没有“if __name__ =='__ main__'的python3.x多处理循环:” - python3.x multiprocessing cycling without “if __name__ == '__main__':” Python:在其中找不到“__main__”模块 - Python: Can't find '__main__' module in
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM