之前使用Tkinter模块时,对于定时器的需求,使用模块中的after,但是随着时间的需求,譬如,最近需要定时20ms调用一个函数,发现after总是在接近40ms的时候才调用。
在此记录一种较为精确的定时器。
class threading.Timer(interval, function, args=[], kwargs={})
创建一个timer,在interval秒过去之后,它将以参数args和关键字参数kwargs运行function 。
from threading import Timer class test: def __init__(self): self.t = Timer(0.02, self.func) self.t.start() def func(self): print 'hello' self.t.cancel() self.t = Timer(0.02, self.func) self.t.start() t = test()