I have a function that occasionally hangs. Normally I would set an alarm, but I'm in Windows and it's unavailable. Is there a simple way around this, or should I just create a thread that calls time.sleep()?
Ended up going with a thread. Only trick was using os._exit
instead of sys.exit
import os
import time
import threading
class Alarm (threading.Thread):
def __init__ (self, timeout):
threading.Thread.__init__ (self)
self.timeout = timeout
self.setDaemon (True)
def run (self):
time.sleep (self.timeout)
os._exit (1)
alarm = Alarm (4)
alarm.start ()
time.sleep (2)
del alarm
print 'yup'
alarm = Alarm (4)
alarm.start ()
time.sleep (8)
del alarm
print 'nope' # we don't make it this far