title: Python: Catch Ctrl-C command. Prompt "really want to quit (y/n)", resume execution if no
body: <p>I have a program that may have a lengthy execution.  In the main module I have the following:  </p>
  
  <pre><code>import signal
  def run_program()
     ...time consuming execution...
  
  def Exit_gracefully(signal, frame):
      ... log exiting information ...
      ... close any open files ...
      sys.exit(0)
  
  if __name__ == '__main__':
      signal.signal(signal.SIGINT, Exit_gracefully)
      run_program()
  </code></pre>
  
  <p>This works fine, but I'd like the possibility to pause execution upon catching SIGINT, prompting the user if they would really like to quit, and resuming where I left off in run_program() if they decide they don't want to quit.</p>
  
  <p>The only way I can think of doing this is running the program in a separate thread, keeping the main thread waiting on it and ready to catch SIGINT.  If the user wants to quit the main thread can do cleanup and kill the child thread.</p>
  
  <p>Is there a simpler way?</p>