Tengo una función que de vez en cuando cuelga. Normalmente, pondría una alarma, pero estoy en Windows y no está disponible. ¿Hay una manera simple de evitar esto, o debería simplemente crear un hilo que llame a time.sleep()?signal.alarm replacement en Windows [Python]
Terminó yendo con un hilo. Único truco estaba usando os._exit
en lugar de 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
Podría, por favor retire la respuesta de su pregunta * * y que lo ponga como un real ... * * respuesta? – ThiefMaster
usted podría [usar 'threading.Timer()' en su lugar] (http://stackoverflow.com/a/28081214/4279) – jfs
@ThiefMaster hecho! – LondonRob