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

Python 这个看门狗功能是如何工作的?,python,watchdog,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.han

这是从这个网站上找到的一个例子。 我不清楚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()

基本上,这是一个终止代码的技巧,需要很长时间才能执行。是的,但它似乎不起作用。它似乎
self.timer.start()
丢失了,我该把它放在哪里?在实例化Watchdog之后放置它是不起作用的。它确实引起了一个错误,但它并没有打破循环。你能给出你在哪里找到代码的参考资料吗。这似乎是不完整的