如果父项在Python中被杀死,则杀死子进程 [英] Kill Child Process if Parent is killed in Python

查看:286
本文介绍了如果父项在Python中被杀死,则杀死子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从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.

这篇关于如果父项在Python中被杀死,则杀死子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆