cancel join after sys.exit in multiprocessing

冷暖自知 提交于 2019-12-23 19:13:39

问题


On OSX I create a tree of processes with multiprocessing.Process. When I send a signal to a parent process, the process enters a join state:

[INFO/MainProcess] process shutting down
[INFO/MainProcess] calling join() for process Process-1

I am already catching the signal with a signal handler and then calling sys.exit(1). Is there something I can call before sys.exit(1) that will prevent this process from waiting for its child?


回答1:


You can avoid this by setting the daemon property to True on your child processes. From the multiprocessing.Process docs (emphasis mine):

daemon

The process’s daemon flag, a Boolean value. This must be set before start() is called.

The initial value is inherited from the creating process.

When a process exits, it attempts to terminate all of its daemonic child processes.

Note that a daemonic process is not allowed to create child processes. Otherwise a daemonic process would leave its children orphaned if it gets terminated when its parent process exits. Additionally, these are not Unix daemons or services, they are normal processes that will be terminated (and not joined) if non-daemonic processes have exited.

So if p.daemon == True, your parent process will just kill your child process, rather than join it. Note, though, that your daemonic processes cannot create their own children (as stated in the docs).




回答2:


I solved this by using os._exit(1) instead of sys.exit(1).



来源:https://stackoverflow.com/questions/23934399/cancel-join-after-sys-exit-in-multiprocessing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!