how to time-out a statement/block of code in python?
I tried using the below looking at a post but it couldn't identify the signal:
wls:/offline> import signal
Traceback (innermost last):
File "<console>", line 1, in ?
ImportError: no module named signal
code:
import os
import signal
def handler(signum, frame):
print "Forever is over!"
raise Exception("end of time")
def loop_forever():
import time
while 1:
print "sec"
time.sleep(1)
signal.signal(signal.SIGALRM, handler)
signal.alarm(10)
try:
loop_forever()
except Exception, exc:
print exc
This seems to be working
Which calls a method after the timeout:
from threading import Timer
import time
def timeout():
print 'in timeout'
def main():
print 'it keeps going and going ',
while 1:
print 'and going '
time.sleep(3)
try:
Timer(5.0,timeout).start()
main()
except:
print "whoops"