`

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

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics