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

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-25
  • 2021-12-06
  • 2022-12-23
  • 2021-07-03
  • 2022-01-13
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-03-08
  • 2021-05-20
  • 2021-07-24
相关资源
相似解决方案