Python: 在线程之间共享一个列表

Python: sharing a list between threads

我试图了解线程如何与 space 在同一进程中 运行 共享时共享的变量进行交互的细微差别。下面的代码显示了两个函数,prod 和 consum,它们在不同的线程中分离。这两个函数被赋予了相同的列表和锁参数:它们使用 da_list 来共享数据,它们使用 lockguy 来 synchronize/thread-safe 共享。

我的主要问题是:当我 运行 这段代码时,它打印出 (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 先前绑定到的对象。它没有。

上面的行创建了一个新列表并将本地名称 l 绑定到它。 l 之前可能绑定的任何对象都不再与名称 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] = 2b = [0] 的区别。在第一个中,修改了 b 绑定的对象。在第二个中,b 绑定到一个全新的对象。

同样,代码中的 l = [0]l 绑定到一个新对象,您已经丢失并且无法重新获得对原始对象的任何引用。