使用python-daemon时,我正在创建子进程,如下所示:
import multiprocessing
class Worker(multiprocessing.Process):
def __init__(self, queue):
self.queue = queue # we wait for things from this in Worker.run()
...
q = multiprocessing.Queue()
with daemon.DaemonContext():
for i in xrange(3):
Worker(q)
while True: # let the Workers do their thing
q.put(_something_we_wait_for())
当我用 Ctrl-C 或 SIGTERM 等杀死父守护进程(即不是 Worker)时,孩子不会死。一个人如何杀死孩子?
我的第一个想法是使用atexit杀死所有的工人,就像这样:
with daemon.DaemonContext():
workers = list()
for i in xrange(3):
workers.append(Worker(q))
@atexit.register
def kill_the_children():
for w in workers:
w.terminate()
while True: # let the Workers do their thing
q.put(_something_we_wait_for())
然而,守护进程的子进程处理起来很棘手,我有义务就如何完成这件事提出想法和意见。
谢谢你。