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等等,孩子們不會死。如何殺死孩子?
我首先想到的是用atexit殺死所有的工人,likeso:
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())
然而,守護的孩子們棘手的事情要處理,而且我不得不爲的想法和投入如何這應該完成。
謝謝。
殺死你的孩子確實看起來像一個「守護神」的事情...... – ewall 2010-03-30 03:29:40
絕對。這個守護進程*不符合規範。 – 2010-04-07 14:58:16
這不是Python嗎?你不能只從「邪惡的進口殺嬰」或什麼東西? – Syntactic 2010-04-07 15:00:32