【发布时间】:2017-12-02 04:27:04
【问题描述】:
我很好奇为什么下面代码中的 while 循环在我告诉它等待事件对象之后仍然运行。
import threading, time
msg = threading.Event()
def sec():
print "second thread starting now.."
counter = 0
while True:
#while msg.isSet():
print "hello"
print counter
counter += 1
time.sleep(3)
msg.wait()
secThread = threading.Thread(target = sec)
secThread.start()
print "a"
msg.set()
time.sleep(5)
print "b"
msg.set()
我得到以下输出
second thread starting now..a
hello
0
hello
1
b
hello
2
hello
3
....
(keeps going)
我可以用注释掉的 while 循环来修复它,但我很好奇它为什么仍然运行。主线程已完成运行,因此不应设置线程事件以允许第二个线程运行。
谢谢。
【问题讨论】:
标签: python multithreading