問題描述
我從 python 腳本中生成 5 個不同的進程,如下所示:
I'm spawning 5 different processes from a python script, like this:
p = multiprocessing.Process(target=some_method,args=(arg,))
p.start()
我的問題是,當父進程(主腳本)以某種方式被殺死時,子進程繼續運行.
My problem is, when, somehow the parent process (the main script) gets killed, the child processes keeps on running.
當父進程被殺死時,有沒有辦法殺死像這樣產生的子進程?
Is there a way to kill child processes, which are spawned like this, when the parent gets killed ?
我正在嘗試這個:
p = multiprocessing.Process(target=client.start,args=(self.query_interval,))
p.start()
atexit.register(p.terminate)
但這似乎不起作用
推薦答案
我自己也遇到過同樣的問題,我的解決方法如下:
I've encounter the same problem myself, I've got the following solution:
在調用p.start()
之前,你可以設置p.daemon=True
.然后在這里提到 python.org 多處理
before calling p.start()
, you may set p.daemon=True
. Then as mentioned here python.org multiprocessing
當一個進程退出時,它會嘗試終止其所有守護進程.
When a process exits, it attempts to terminate all of its daemonic child processes.
這篇關于如果 Parent 在 Python 中被殺死,則殺死子進程的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!