install Ctrl-C handler

U

Uwe Mayer

Hi,

can anybody tell me how you install something like a Ctrl-C handler in
Python, i.e. a routine that is called when ^C is pressed and that can do
cleanup work?

Thanks
Uwe
 
J

James Henderson

Hi,

can anybody tell me how you install something like a Ctrl-C handler in
Python, i.e. a routine that is called when ^C is pressed and that can do
cleanup work?

Thanks
Uwe

Maybe you require something more sophisticated but ctrl-C raises an exception
that can be caught:
.... while 1: pass
.... except KeyboardInterrupt:
.... print 'Interrupted!'
....
Interrupted!


James
 
J

James Henderson

Maybe you require something more sophisticated but ctrl-C raises an
exception

that can be caught:

... while 1: pass
... except KeyboardInterrupt:
... print 'Interrupted!'
...
Interrupted!

Thinking about it, try-finally would also work:
.... while True: pass
.... finally:
.... print 'cleaning up'
....
cleaning up
Traceback (most recent call last):
File "<stdin>", line 2, in ?
KeyboardInterrupt


J.
 
D

Diez B. Roggisch

can anybody tell me how you install something like a Ctrl-C handler in
Python, i.e. a routine that is called when ^C is pressed and that can do
cleanup work?

On *nix you can use the signal-module for general signal handling. There are
some caveats in multithreaded apps.

Regards,

Diez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top