问题描述
我有一个主流程,它分叉了许多子流程.我希望能够在我的主进程收到kill信号时杀死这些子进程.理想情况下,我想按照以下方式进行操作:
I have have a main process that forks a number of subprocesses. I want to be able to kill these child processes off when my main process gets the kill signal. Ideally I would want to do something along the lines of:
def handler(signum, frame, pid_list):
log('Killing Process')
for pid in pid_list:
os.kill(pid, signal.SIGTERM)
os.waitpid(pid, 0) # need
sys.exit()
if __name__ == "__main__":
<code that creates child processes, pids>
signal.signal(signal.SIGTERM, handler(pid_list))
但是,那当然行不通……有什么建议吗?
But of course, that doesn't work... any suggestions?
推荐答案
为,您可以在使用multiprocessing
模块创建的子进程中设置daemon=True
标志.要将其安装在python2.4上,请输入:pip install multiprocessing
.
As @tony suggested you could set daemon=True
flag on a child process created using multiprocessing
module. To install it on python2.4, type: pip install multiprocessing
.
如果主进程被信号杀死,则子进程不会终止,因此您需要提供适当的信号处理程序:
The child processes won't be terminated if the main process is killed by a signal so you need to provide an appropriate signal handler:
#!/usr/bin/env python
import logging, signal, sys, time
import multiprocessing as mp # `pip install multiprocessing` on Python <2.6
class AddProcessNameFilter(logging.Filter):
"""Add missing on Python 2.4 `record.processName` attribute."""
def filter(self, r):
r.processName = getattr(r, 'processName', mp.current_process().name)
return logging.Filter.filter(self, r)
def print_dot():
while True:
mp.get_logger().info(".")
time.sleep(1)
def main():
logger = mp.log_to_stderr()
logger.setLevel(logging.INFO)
logger.addFilter(AddProcessNameFilter()) # fix logging records
# catch TERM signal to allow finalizers to run and reap daemonic children
signal.signal(signal.SIGTERM, lambda *args: sys.exit(-signal.SIGTERM))
# create daemonic child processes
processes = [mp.Process(target=print_dot) for _ in range(2)]
for p in processes:
p.daemon = True
p.start()
print_dot()
if __name__=="__main__":
mp.freeze_support()
main()
这篇关于Python在关闭主进程时关闭子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!