Python每隔一秒鍾打印當地時間


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()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM