禁用“加入”当进程关闭时

发布于 2024-08-22 05:36:00 字数 359 浏览 1 评论 0原文

有没有办法阻止 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人海汹涌 2024-08-29 05:36:00

您是否尝试显式使用 Process.terminate

Have you tried to explicitly using Process.terminate?

阳光下的泡沫是彩色的 2024-08-29 05:36:00

您可以尝试加入一个超时(1 秒?)的循环,并检查线程是否仍然存在,例如:

while True:
  a_thread.join(1)
  if not a_thread.isAlive(): break

终止 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:

while True:
  a_thread.join(1)
  if not a_thread.isAlive(): break

Terminating the a_thread will trigger break clause.

杀手六號 2024-08-29 05:36:00

听起来设置子进程的标志 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文