title: Keyboard Interrupts with python's multiprocessing Pool
body: <p>How can I handle KeyboardInterrupt events with python's multiprocessing Pools? Here is a simple example:</p>
  
  <pre><code>from multiprocessing import Pool
  from time import sleep
  from sys import exit
  
  def slowly_square(i):
      sleep(1)
      return i*i
  
  def go():
      pool = Pool(8)
      try:
          results = pool.map(slowly_square, range(40))
      except KeyboardInterrupt:
          # **** THIS PART NEVER EXECUTES. ****
          pool.terminate()
          print "You cancelled the program!"
          sys.exit(1)
      print "\nFinally, here are the results: ", results
  
  if __name__ == "__main__":
      go()
  </code></pre>
  
  <p>When running the code above, the <code>KeyboardInterrupt</code> gets raised when I press <code>^C</code>, but the process simply hangs at that point and I have to kill it externally.</p>
  
  <p>I want to be able to press <code>^C</code> at any time and cause all of the processes to exit gracefully.</p>