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?
As @tony suggested you could set
daemon=True
flag on a child process created usingmultiprocessing
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:
What about use this flag when you create a subprocess?