我想用两个用例在 Python 中实现一个简单的看门狗定时器:
- 看门狗确保函数执行时间不超过
x
秒 - 看门狗确保某些定期执行的功能确实至少每秒钟执行
y
一次
我怎么做?
只是发布我自己的解决方案:
from threading import Timer
class Watchdog(Exception):
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()
signal.alarm()
为您的程序设置超时,您可以在主循环中调用它,并将其设置为您准备容忍的两次中的较大者:
import signal
while True:
signal.alarm(10)
infloop()
这是我在没有课程的应用程序中使用的 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)