我试图了解线程在同一进程中运行时如何与它们共享的变量空间交互的细微差别。下面的代码显示了两个函数,prod 和 consum,它们在不同的线程中分离。这两个函数被赋予了相同的列表和锁参数:它们使用 da_list 来共享数据,它们使用 lockguy 来同步/线程安全地共享。
我的主要问题是:当我运行这段代码时,它打印出 (1, 0, 0, 0, 0 ...) 而不是 (1,2,3,4,5,6 ...)我期待着。当我删除 consum 函数中的 l =[0] 行时,我得到了预期的行为。为什么 l = [0] 搞砸了?当consum结束时,da_list应该是[0]。 prod 的后续调用应将 da_list 重置为 [da_int]。感谢您的帮助。
import threading
import time
def prod(l,lock):
da_int = 0
while True:
with lock:
time.sleep(0.1)
da_int += 1
l[0] = da_int
def consum(l,lock):
data = ''
while True:
with lock:
time.sleep(0.1)
print(l[0])
l = [0]
def main():
da_list = [0]
lockguy = threading.Lock()
thread1 = threading.Thread(target = prod, args=(da_list,lockguy))
thread2 = threading.Thread(target = consum, args=(da_list,lockguy))
thread1.daemon = True
thread2.daemon = True
thread1.start()
thread2.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
pass
finally:
print('done')
if __name__ == '__main__':
main()
最佳答案
l = [0]
您似乎对 Python 中赋值的工作方式感到困惑。您似乎认为上面的行修改了 l
先前绑定(bind)到的对象。它没有。
上面的行创建了一个新列表并将本地名称 l
绑定(bind)到它。无论对象 l
之前可能绑定(bind)到什么,都不再与名称 l
相关。在此范围内对 l
的任何后续使用都将引用这个新创建的列表。
考虑这个单线程代码:
a = b = [1] # a and b are both bound to the same list
print a,b # [1] [1]
b[0] = 2 # modifies the object to which a and b are bound
print a,b # [2] [2]
b = [0] # now b is bound to a new list
print a,b # [2] [0]
请注意 b[0] = 2
和 b = [0]
有何不同。在第一个中,修改了 b
绑定(bind)的对象。在第二个中,b
绑定(bind)到一个全新的对象。
同样,代码中的 l = [0]
将 l
绑定(bind)到一个新对象,您已经丢失并且无法重新获得对原始对象的任何引用。
关于Python:在线程之间共享一个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28199359/