RosettaCodeData/Task/Handle-a-signal/Python/handle-a-signal-3.py

30 lines
535 B
Python
Raw Permalink Normal View History

2015-02-20 00:35:01 -05:00
import signal, time, threading
done = False
n = 0
2013-04-10 21:29:02 -07:00
2015-02-20 00:35:01 -05:00
def counter():
global n, timer
n += 1
print n
timer = threading.Timer(0.5, counter)
timer.start()
2013-04-10 21:29:02 -07:00
def sigIntHandler(signum, frame):
2015-02-20 00:35:01 -05:00
global done
timer.cancel()
done = True
2013-04-10 21:29:02 -07:00
2015-02-20 00:35:01 -05:00
def intrptUNIX():
global timer
signal.signal(signal.SIGINT, sigIntHandler)
2013-04-10 21:29:02 -07:00
2015-02-20 00:35:01 -05:00
timer = threading.Timer(0.5, counter)
timer.start()
while not done:
signal.pause()
2013-04-10 21:29:02 -07:00
2015-02-20 00:35:01 -05:00
t1 = time.time()
intrptUNIX()
2013-04-10 21:29:02 -07:00
tdelt = time.time() - t1
print 'Program has run for %5.3f seconds.' % tdelt