我想在Python中实现一个带有两个用例的简单监视程序计时器:
x
秒y
秒我该怎么做?
答案 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)