直接贴代码,没怎么测试,后面再优化:
#encoding: UTF-8
import threading
import time
class Timer(threading.Thread):
def __init__(self, PauseTime, func, *args, **kwargs):
super(Timer, self).__init__()
self.PauseTime = PauseTime
self.func = func
self.args = args
self.kwargs = kwargs
def run(self):
while 1:
apply(self.func, self.args, self.kwargs)
time.sleep(self.PauseTime)
def sayHello(a, b, c=1):
print "Hello"
if __name__ == "__main__":
ATimer = Timer(10, sayHello, [1,2], {"c": 10})
ATimer.start()
BTimer = Timer(5, sayHello, [3,4], {"a": 20})
BTimer.start()