बस इस के लिए अपने खुद के समाधान का प्रकाशन:
watchdog = Watchdog(x)
try:
# do something that might take too long
except Watchdog:
# handle watchdog error
watchdog.stop()
प्रयोग करता है, तो आप नियमित रूप से कुछ पर अमल और करना चाहते हैं:
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
सेकंड में खत्म करना चाहते हैं सुनिश्चित करें कि इसे कम से कम प्रत्येक 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()
स्रोत
2013-04-22 13:46:37
यहाँ जो केवल एक धागा बनाता है] (https://stackoverflow.com/a/34115590/4279) – jfs