Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching KeyboardInterrupt in Python during program shutdown

I'm writing a command line utility in Python which, since it is production code, ought to be able to shut down cleanly without dumping a bunch of stuff (error codes, stack traces, etc.) to the screen. This means I need to catch keyboard interrupts.

I've tried using both a try catch block like:

if __name__ == '__main__':     try:         main()     except KeyboardInterrupt:         print 'Interrupted'         sys.exit(0) 

and catching the signal itself (as in this post):

import signal import sys  def sigint_handler(signal, frame):     print 'Interrupted'     sys.exit(0) signal.signal(signal.SIGINT, sigint_handler) 

Both methods seem to work quite well during normal operation. However, if the interrupt comes during cleanup code at the end of the application, Python seems to always print something to the screen. Catching the interrupt gives

^CInterrupted Exception KeyboardInterrupt in <bound method MyClass.__del__ of <path.to.MyClass object at 0x802852b90>> ignored 

whereas handling the signal gives either

^CInterrupted Exception SystemExit: 0 in <Finalize object, dead> ignored 

or

^CInterrupted Exception SystemExit: 0 in <bound method MyClass.__del__ of <path.to.MyClass object at 0x802854a90>> ignored 

Not only are these errors ugly, they're not very helpful (especially to an end user with no source code)!

The cleanup code for this application is fairly big, so there's a decent chance that this issue will be hit by real users. Is there any way to catch or block this output, or is it just something I'll have to deal with?

like image 445
Dan Avatar asked Jan 14 '14 18:01

Dan


People also ask

How do I fix KeyboardInterrupt in Python?

In Python, there is no special syntax for the KeyboardInterrupt exception; it is handled in the usual try and except block. The code that potentially causes the problem is written inside the try block, and the 'raise' keyword is used to raise the exception, or the python interpreter raises it automatically.

How does Python detect KeyboardInterrupt?

Interpreter in python checks regularly for any interrupts while executing the program. In python, interpreter throws KeyboardInterrupt exception when the user/programmer presses ctrl – c or del key either accidentally or intentionally.

What does KeyboardInterrupt mean in Python?

The KeyboardInterrupt error occurs when a user manually tries to halt the running program by using the Ctrl + C or Ctrl + Z commands or by interrupting the kernel in the case of Jupyter Notebook.

What does Ctrl C do in Python?

Python allows us to set up signal -handlers so when a particular signal arrives to our program we can have a behavior different from the default. For example when you run a program on the terminal and press Ctrl-C the default behavior is to quit the program.


1 Answers

Checkout this thread, it has some useful information about exiting and tracebacks.

If you are more interested in just killing the program, try something like this (this will take the legs out from under the cleanup code as well):

if __name__ == '__main__':     try:         main()     except KeyboardInterrupt:         print('Interrupted')         try:             sys.exit(0)         except SystemExit:             os._exit(0) 
like image 71
Dan Hogan Avatar answered Sep 20 '22 17:09

Dan Hogan