Handle a signal: Difference between revisions

Line 179:
t1 = time.time()
intrptUNIX()
tdelt = time.time() - t1
print 'Program has run for %5.3f seconds.' % tdelt</python>
 
How about this one? It should work on all platforms; and it does show how to install a signal handler:
<python>import time, signal
 
class WeAreDoneException(Exception):
pass
 
def sigIntHandler(signum, frame):
raise WeAreDoneException
 
signal.signal(signal.SIGINT, sigIntHandler)
 
t1 = time.time()
 
try:
n = 0
while True:
time.sleep(0.5)
n += 1
print n
except WeAreDoneException:
pass
 
tdelt = time.time() - t1
print 'Program has run for %5.3f seconds.' % tdelt</python>
Anonymous user