solo la pubblicazione di mia soluzione a questo:
from threading import Timer
class Watchdog:
def __init__(self, timeout, userHandler=None): # timeout in seconds
self.timeout = timeout
self.handler = userHandler if userHandler is not None else self.defaultHandler
self.timer = Timer(self.timeout, self.handler)
self.timer.start()
def reset(self):
self.timer.cancel()
self.timer = Timer(self.timeout, self.handler)
self.timer.start()
def stop(self):
self.timer.cancel()
def defaultHandler(self):
raise self
Utilizzo Se si vuole fare in modo la funzione termina in meno di x
secondi:
watchdog = Watchdog(x)
try:
# do something that might take too long
except Watchdog:
# handle watchdog error
watchdog.stop()
Utilizzo Se si esegue regolarmente qualcosa e vuole assicurati che sia eseguito almeno ogni y
secondi:
import sys
def myHandler():
print "Whoa! Watchdog expired. Holy heavens!"
sys.exit()
watchdog = Watchdog(y, myHandler)
def doSomethingRegularly():
# make sure you do not return in here or call watchdog.reset() before returning
watchdog.reset()
fonte
2013-04-22 13:46:37
Ecco [ 'implementazione WatchdogTimer' che crea solo un thread] (https://stackoverflow.com/a/34115590/4279) – jfs