禁用“加入”当进程关闭时
有没有办法阻止 multiprocessing
Python 模块尝试调用 &等待父进程的子进程关闭时的 join() 吗?
2010-02-18 10:58:34,750 INFO 为进程 procRx1 调用 join()
我希望向其发送 SIGTERM
的进程尽快退出(即“快速失败”),而不是等待几秒钟才最终放弃加入
尝试。
澄清:我有一个“中央流程”,它创建了一堆“子流程”。我正在寻找一种方法来干净地处理来自任何进程的“SIGTERM”信号,以便关闭整个进程树。
Is there a way to stop the multiprocessing
Python module from trying to call & wait on join()
on child processes of a parent process shutting down?
2010-02-18 10:58:34,750 INFO calling join() for process procRx1
I want the process to which I sent a SIGTERM
to exit as quickly as possible (i.e. "fail fast") instead of waiting for several seconds before finally giving up on the join
attempt.
Clarifications: I have a "central process" which creates a bunch of "child processes". I am looking for a way to cleanly process a "SIGTERM" signal from any process in order to bring down the whole process tree.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试显式使用
Process.terminate
?Have you tried to explicitly using
Process.terminate
?您可以尝试加入一个超时(1 秒?)的循环,并检查线程是否仍然存在,例如:
终止
a_thread
将触发break
子句。You could try joining in a loop with a timeout (1 sec?) and checking if the thread is still alive, something like:
Terminating the
a_thread
will triggerbreak
clause.听起来设置子进程的标志
Process.daemon = False
可能就是您想要的:Process.daemon:
进程的守护进程标志,布尔值。必须在调用 start() 之前设置它。
初始值是从创建过程继承的。
当进程退出时,它会尝试终止其所有守护子进程。
请注意,守护进程不允许创建子进程。否则,如果守护进程在其父进程退出时终止,则其子进程将成为孤立的。此外,这些不是 Unix 守护进程或服务,它们是正常进程,如果非守护进程退出,它们将被终止(并且不会加入)。
Sounds like setting your subprocess' flag
Process.daemon = False
may be what you want:Process.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-dameonic processes have exited.