title: Python multiprocessing: Kill worker on exit
body: <p>I have written a simple function to run jobs with dependency tracking. The exact code isn't important, but the way I did it is to create a job monitoring function that I fork with <code>multiprocessing.Process</code>, I send and get jobs to/from it with two multiprocessing <code>Queue</code> objects.</p>
  
  <p>It works great, but because I use an infinite loop, when the parent process hangs on exit, because python is still waiting for the child. Is there a good way to kill a child process immediately on exit? Maybe by catching a signal?</p>
  
  <p>My actual code is here:
  <a href="https://gist.github.com/MikeDacre/e672969aff980ee950b9dfa8b2552d40" rel="nofollow">https://gist.github.com/MikeDacre/e672969aff980ee950b9dfa8b2552d40</a>
  A more complete example is here:
  <a href="http://nbviewer.jupyter.org/github/MikeDacre/python-cluster/blob/cluster/tests/local_queue.ipynb" rel="nofollow">http://nbviewer.jupyter.org/github/MikeDacre/python-cluster/blob/cluster/tests/local_queue.ipynb</a></p>
  
  <p>A toy example is probably better though:</p>
  
  <pre><code>import multiprocessing as mp
  from time import sleep
  def runner():
      while True:
          sleep(2)
  
  runner = mp.Process(target=runner)
  runner.start()
  exit()
  </code></pre>
  
  <p>That will happily hang until Ctrl-C is entered.</p>
  
  <p>I don't think signal catching will work, as there are no signals sent on normal exit. Is there any way to catch <code>exit()</code>? If not is there any way to create a Process in a way that will terminate naturally?</p>