How do I capture SIGINT in Python?
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Track title: CC B Schuberts Piano Sonata No 16 D
--
Chapters
00:00 Question
00:36 Accepted answer (Score 968)
01:00 Answer 2 (Score 200)
01:22 Answer 3 (Score 78)
02:41 Answer 4 (Score 33)
02:59 Thank you
--
Full question
https://stackoverflow.com/questions/1112...
Accepted answer links:
[here]: http://www.linuxjournal.com/article/3946
[here]: http://docs.python.org/library/signal.ht...
Answer 3 links:
https://gist.github.com/2907502
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #controls #signals
#avk47
--
Track title: CC B Schuberts Piano Sonata No 16 D
--
Chapters
00:00 Question
00:36 Accepted answer (Score 968)
01:00 Answer 2 (Score 200)
01:22 Answer 3 (Score 78)
02:41 Answer 4 (Score 33)
02:59 Thank you
--
Full question
https://stackoverflow.com/questions/1112...
Accepted answer links:
[here]: http://www.linuxjournal.com/article/3946
[here]: http://docs.python.org/library/signal.ht...
Answer 3 links:
https://gist.github.com/2907502
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #controls #signals
#avk47
ACCEPTED ANSWER
Score 1056
Register your handler with signal.signal like this:
#!/usr/bin/env python
import signal
import sys
def signal_handler(sig, frame):
print('You pressed Ctrl+C!')
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
print('Press Ctrl+C')
signal.pause()
Code adapted from here.
More documentation on signal can be found here.
ANSWER 2
Score 216
You can treat it like an exception (KeyboardInterrupt), like any other. Make a new file and run it from your shell with the following contents to see what I mean:
import time, sys
x = 1
while True:
try:
print x
time.sleep(.3)
x += 1
except KeyboardInterrupt:
print "Bye"
sys.exit()
ANSWER 3
Score 82
And as a context manager:
import signal
class GracefulInterruptHandler(object):
def __init__(self, sig=signal.SIGINT):
self.sig = sig
def __enter__(self):
self.interrupted = False
self.released = False
self.original_handler = signal.getsignal(self.sig)
def handler(signum, frame):
self.release()
self.interrupted = True
signal.signal(self.sig, handler)
return self
def __exit__(self, type, value, tb):
self.release()
def release(self):
if self.released:
return False
signal.signal(self.sig, self.original_handler)
self.released = True
return True
To use:
with GracefulInterruptHandler() as h:
for i in xrange(1000):
print "..."
time.sleep(1)
if h.interrupted:
print "interrupted!"
time.sleep(2)
break
Nested handlers:
with GracefulInterruptHandler() as h1:
while True:
print "(1)..."
time.sleep(1)
with GracefulInterruptHandler() as h2:
while True:
print "\t(2)..."
time.sleep(1)
if h2.interrupted:
print "\t(2) interrupted!"
time.sleep(2)
break
if h1.interrupted:
print "(1) interrupted!"
time.sleep(2)
break
From here: https://gist.github.com/2907502
ANSWER 4
Score 33
You can handle CTRL+C by catching the KeyboardInterrupt exception. You can implement any clean-up code in the exception handler.