5
所以我有这样的代码:在线程之间传递变量的变化在Python函数[新手]
import time
import threading
bar = False
def foo():
while True:
if bar == True:
print "Success!"
else:
print "Not yet!"
time.sleep(1)
def example():
while True:
time.sleep(5)
bar = True
t1 = threading.Thread(target=foo)
t1.start()
t2 = threading.Thread(target=example)
t2.start()
我试图理解为什么我不能让bar
到=
到true
。如果是这样,那么另一个线程应该看到更改并写入Success!
这两个函数中的'bar'不在同一个范围内。在学习多线程之前,你应该处理范围。无论如何,应该有可用于线程的互相资源构造。 – 2013-03-06 17:58:47
'time.sleep(1)'中的缩进是错误的。我认为是打算在while循环中。 – 2013-03-06 18:21:27