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