这个看门狗功能如何工作?

时间:2017-03-23 14:40:53

标签: python watchdog

这是来自此网站上的示例。 我不清楚在except子句中何时执行Watchdog函数。在我看来,除非出现错误,否则永远不会执行。我错过了什么?

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()

0 个答案:

没有答案