When using python-daemon, I’m creating subprocesses likeso:
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())
When I kill the parent daemonic process (i.e. not a Worker) with a Ctrl-C or SIGTERM, etc., the children don’t die. How does one kill the kids?
My first thought is to use atexit to kill all the workers, 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())
However, the children of daemons are tricky things to handle, and I’d be obliged for thoughts and input on how this ought to be done.
Thank you.
Your options are a bit limited. If doing
self.daemon = Truein the constructor for theWorkerclass does not solve your problem and trying to catch signals in the Parent (ie,SIGTERM, SIGINT) doesn’t work, you may have to try the opposite solution – instead of having the parent kill the children, you can have the children commit suicide when the parent dies.The first step is to give the constructor to
WorkerthePIDof the parent process (you can do this withos.getpid()). Then, instead of just doingself.queue.get()in the worker loop, do something like this:The solution above checks to see if the parent PID is different than what it originally was (that is, if the child process was adopted by
initorlauchdbecause the parent died) – see reference. However, if that doesn’t work for some reason you can replace it with the following function (adapted from here):Now, when the Parent dies (for whatever reason), the child Workers will spontaneously drop like flies – just as you wanted, you daemon!
:-D