title: Python close children when closing main process
body: <p>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: </p>
  
  <pre><code>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__":
      &lt;code that creates child processes, pids&gt;
      signal.signal(signal.SIGTERM, handler(pid_list))
  </code></pre>
  
  <p>But of course, that doesn't work... any suggestions?</p>