Python每隔一秒钟打印当地时间

℡╲_俬逩灬. 提交于 2019-11-29 20:41:27
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()

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!