import threading,time global t def sayHello(): print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) t=threading.Timer(1.0,sayHello) t.start() t=threading.Timer(1.0,sayHello) t.start()
分析一下以上程序,其實,第二個
t=threading.Timer(1.0,sayHello) t.start()
僅僅是為了進入sayHello函數,進入該函數之后,sayHello自己就進入了一個無限循環過程,重復每隔一秒鍾運行自己,這樣便有了計時器的感覺。
所以程序可以這樣寫:
import threading,time global t def sayHello(): print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) t=threading.Timer(1.0,sayHello) t.start() # t=threading.Timer(1.0,sayHello) # t.start() sayHello()