title: Cleanly Exit on Ctrl+C
body: <p>When an exception occurs, I want to cleanly exit without partially processed data.  This is the sort of code that I'm trying:</p>
  
  <pre><code>exit_lock = threading.lock()
  def sig_interrupt_handler(signal, frame):
      with exit_lock:
          sys.exit(0)
  
  signal.signal(signal.SIGINT, sig_interrupt_handler)
  
  while data_left() &gt; 0:
      with exit_lock:
          data = fetch_data(10)
          for datum in data:
              processor = Processor(datum)
              processor.process()
              datum.delete()
  </code></pre>
  
  <p>However, this code causes my program to lock up when ctrl+c is pressed.  I've tried having the interrupt handler spin off a new thread, but this causes ctrl+c to do nothing.</p>
  
  <p>What is the correct way to ensure a clean exit?</p>