Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python线程重复计时器:它可以工作,但我做错了什么吗?_Python_Multithreading_Timer - Fatal编程技术网

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

Python线程重复计时器:它可以工作,但我做错了什么吗?,python,multithreading,timer,Python,Multithreading,Timer,我正在尝试创建一个重复线程.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 = threadin

我正在尝试创建一个重复线程.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
所以我的问题是,我是否做了坏事

谢谢你的关注


Dave,线程无法共享全局文件。除非使用线程安全队列共享数据,否则每个线程的变量都是唯一的。