By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,354 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

install Ctrl-C handler

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Wednesday 04 February 2004 12:04 pm, Uwe Mayer wrote:
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:
try:

.... while 1: pass
.... except KeyboardInterrupt:
.... print 'Interrupted!'
....
Interrupted!
James
--
James Henderson, Logical Progression Ltd.
http://www.logicalprogression.net/
http://sourceforge.net/projects/mailmanager/
Jul 18 '05 #2

P: n/a
On Wednesday 04 February 2004 12:15 pm, James Henderson wrote:
On Wednesday 04 February 2004 12:04 pm, Uwe Mayer wrote:
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:
try:
... while 1: pass
... except KeyboardInterrupt:
... print 'Interrupted!'
...
Interrupted!


Thinking about it, try-finally would also work:
try:

.... while True: pass
.... finally:
.... print 'cleaning up'
....
cleaning up
Traceback (most recent call last):
File "<stdin>", line 2, in ?
KeyboardInterrupt
J.
--
James Henderson, Logical Progression Ltd.
http://www.logicalprogression.net/
http://sourceforge.net/projects/mailmanager/
Jul 18 '05 #3

P: n/a
> 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
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.