gpt4 book ai didi

python - 在多处理中的 sys.exit 后取消加入

转载 作者:行者123 更新时间:2023-11-28 22:49:20 30 4
gpt4 key购买 nike

在 OSX 上,我使用 multiprocessing.Process 创建了一个进程树。当我向父进程发送信号时,进程进入连接状态:

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

我已经使用信号处理程序捕获信号,然后调用 sys.exit(1)。我可以在 sys.exit(1) 之前调用什么来阻止此进程等待其子进程吗?

最佳答案

您可以通过将子进程的 daemon 属性设置为 True 来避免这种情况。来自multiprocessing.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-daemonic processes have exited.

因此,如果 p.daemon == True,您的父进程将终止您的子进程,而不是加入。但请注意,您的守护进程无法创建自己的子进程(如文档中所述)。

关于python - 在多处理中的 sys.exit 后取消加入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23934399/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com