这是更正的版本:
from __future__ import print_functionfrom threading import Timerdef hello(): print("Hello World!")class RepeatingTimer(object): def __init__(self, interval, f, *args, **kwargs): self.interval = interval self.f = f self.args = args self.kwargs = kwargs self.timer = None def callback(self): self.f(*self.args, **self.kwargs) self.start() def cancel(self): self.timer.cancel() def start(self): self.timer = Timer(self.interval, self.callback) self.timer.start()t = RepeatingTimer(3, hello)t.start()
示例运行:
$ python -i foo.py>>> Hello World!>>> Hello World!>>> t.cancel()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)