It's pretty straightforward to implement yourself:
from multiprocessing import Process, Event
class Timer(Process):
def __init__(self, interval, function, args=[], kwargs={}):
super(Timer, self).__init__()
self.interval = interval
self.function = function
self.args = args
self.kwargs = kwargs
self.finished = Event()
def cancel(self):
"""Stop the timer if it hasn't finished yet"""
self.finished.set()
def run(self):
self.finished.wait(self.interval)
if not self.finished.is_set():
self.function(*self.args, **self.kwargs)
self.finished.set()
I'm not sure why there isn't one included in the stdlib. Perhaps because its less likely to be useful?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…