如何在Python中实现看门狗定时器?

时间:2013-04-22 13:46:18

标签: python watchdog

我想在Python中实现一个带有两个用例的简单监视程序计时器:

  • Watchdog确保函数的执行时间不超过x
  • Watchdog确保某些定期执行的函数确实至少每y
  • 执行一次

我该怎么做?

3 个答案:

答案 0 :(得分:10)

只需发布我自己的解决方案:

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

用法如果要确保函数在x秒内完成:

watchdog = Watchdog(x)
try:
  # do something that might take too long
except Watchdog:
  # handle watchdog error
watchdog.stop()

用法,如果您经常执行某些操作并希望确保至少每y秒执行一次:

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

答案 1 :(得分:1)

signal.alarm()为您的程序设置超时,您可以在主循环中调用它,并将其设置为您准备容忍的两倍中的较大者:

import signal
while True:
    signal.alarm(10)
    infloop()

答案 2 :(得分:0)

这是我在不上课的应用中使用的wdt。它无法阻止它:

from threading import Event, Thread

def wdt(time, callback):
    # a reset flag
    reset_e = Event()
    # a function to reset the wdt
    def reset(): reset_e.set()
    # the function to run in a differen thread
    def checker():
        # check if reset flag is set.
        # wait for specified time to give chance to reset.
        while reset_e.wait(time):
            # it was set in time. clear and wait again
            reset_e.clear()
        # time run out.
        callback()
    # the event is not set by default. Set it
    reset()
    # create and start the wdt
    t = Thread(target=checker)
    t.start()
    # return the resetter
    return reset


# The callback to run if wdt is not reset
def bark():
    print('woof')       

# Test
import time
reset = wdt(1.0, bark)
time.sleep(0.9)
reset()
time.sleep(2.0)