黄智勇 发表于 2017-5-5 06:34:52

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()
页: [1]
查看完整版本: Python每隔一秒钟打印当地时间