Python线程重复计时器:它工作,但我做错了什么?

时间:2014-07-31 11:21:38

标签: python multithreading timer

我正在尝试创建重复的threading.Timer()。我已经读过其他人发布的这个问题(和一些解决方案)。我的解决方案是;

import threading
from time import sleep

def timeExpired():
    global t
    print "Timer expired"
    del t
    t = threading.Timer(3.0, timeExpired)
    t.start()

# MAIN PROGRAM
t = threading.Timer(3.0, timeExpired)
t.start()

while True:
    print "Program started. Sleeping for 10 second"
    sleep(10)

这样做还可以,但是...我写的并不是很好,因为我使用了" global"声明,我也删除了对执行代码的引用。此外,当使用^ C退出时,我收到以下错误消息;

Unhandled exception in thread started by
sys.excepthook is missing
lost sys.stderr

所以我的问题是,我做过坏事吗?

感谢您的关注。

戴夫

0 个答案:

没有答案