Kill Child Process if Parent is killed in Python
The child is not notified of the death of its parent, it only works the other way.
However, when a process dies, all its file descriptors are closed. And the other end of a pipe is notified about this, if it selects the pipe for reading.
So your parent can create a pipe before spawning the process (or in fact, you can just set up stdin to be a pipe), and the child can select that for reading. It will report ready for reading when the parent end is closed. This requires your child to run a main loop, or at least make regular calls to select. If you don't want that, you'll need some manager process to do it, but then when that one is killed, things break again.
I've encounter the same problem myself, I've got the following solution:
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.