This is from an example found on this site.
I am not clear on when the Watchdog
function is executed in the except clause. In my opinion this will never be executed unless there is an error. What am I missing?
from threading import Timer
import time
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)
def reset(self):
self.timer.cancel()
self.timer = Timer(self.timeout, self.handler)
def stop(self):
self.timer.cancel()
def defaultHandler(self):
raise self
watchdog = Watchdog(2)
watchdog.timer.start()
x=1
try:
# do something that might take too long
while x>0:
print "test"
time.sleep(0.2)
except watchdog:
# handle watchdog error
watchdog.stop()