Python:使用多处理从不同进程附加到同一列表

Ada*_*dam 13 python list multiprocessing

我需要使用多处理将对象附加到来自不同进程的一个列表"L",但它返回空列表.如何使用多处理将许多进程附加到列表"L"?

    #!/usr/bin/python
from multiprocessing import Process
L=[]
def dothing(i,j):
        L.append("anything")
        print i
if __name__ == "__main__":
        processes=[]
        for i in range(5):
                p=Process(target=dothing,args=(i,None))
                p.start()
                processes.append(p)
        for p in processes:
                p.join()
print L
Run Code Online (Sandbox Code Playgroud)

fal*_*tru 22

全局变量不在进程之间共享.

你需要使用multiprocessing.Manager.list:

from multiprocessing import Process, Manager

def dothing(L, i):  # the managed list `L` passed explicitly.
    L.append("anything")

if __name__ == "__main__":
    with Manager() as manager:
        L = manager.list()  # <-- can be shared between processes.
        processes = []
        for i in range(5):
            p = Process(target=dothing, args=(L,i))  # Passing the list
            p.start()
            processes.append(p)
        for p in processes:
            p.join()
        print L
Run Code Online (Sandbox Code Playgroud)

请参阅进程之间的共享状态(服务器进程部分).


小智 8

Falsetru 的回答有效。

但是,除了with Manager() as manager:需要进行两项更改之外,仍然无法访问该列表:

  1. L = []if __name__ == "__main__":语句前添加。由于某种原因,必须添加最后一个print(L)(在 之外的if)执行Processes + 1次数。这将返回一个错误,L is not defined并且代码会中断。

  2. L = list(L)p.join()声明之后添加。此步骤需要更改Manager.list为常规Python.list- 否则调用manager.list返回对象不可读的错误。

from multiprocessing import Process, Manager

def dothing(L, i):  # the managed list `L` passed explicitly.
    for j in range(5):
        text = "Process " + str(i) + ", Element " + str(j)
        L.append(text)

L = [] 

if __name__ == "__main__":
    with Manager() as manager:
        L = manager.list()  # <-- can be shared between processes.
        processes = []

        for i in range(5):
            p = Process(target=dothing, args=(L,i,))  # Passing the list
            p.start()
            processes.append(p)

        for p in processes:
            p.join()

        L = list(L) 
        print("Within WITH")
        print(L)

    print("Within IF")
    print(L)

print("Outside of IF")
print(L)
Run Code Online (Sandbox Code Playgroud)

输出:

Outside of IF
[]
Outside of IF
[]
Outside of IF
[]
Outside of IF
[]
Outside of IF
[]
Outside of IF
[]
Within WITH
['Process 2, Element 0','Process 2, Element 1', 'Process 2, Element 2',
'Process 2, Element 3', 'Process 2, Element 4', 'Process 1, Element 0', 
'Process 1, Element 1', 'Process 1, Element 2', 'Process 1, Element 3', 
'Process 1, Element 4', 'Process 0, Element 0', 'Process 0, Element 1', 
'Process 0, Element 2', 'Process 0, Element 3', 'Process 0, Element 4', 
'Process 4, Element 0', 'Process 4, Element 1', 'Process 4, Element 2', 
'Process 4, Element 3', 'Process 4, Element 4', 'Process 3, Element 0', 
'Process 3, Element 1', 'Process 3, Element 2', 'Process 3, Element 3', 
'Process 3, Element 4']

Within IF
['Process 2, Element 0','Process 2, Element 1', 'Process 2, Element 2', 
'Process 2, Element 3', 'Process 2, Element 4', 'Process 1, Element 0', 
'Process 1, Element 1', 'Process 1, Element 2', 'Process 1, Element 3', 
'Process 1, Element 4', 'Process 0, Element 0', 'Process 0, Element 1', 
'Process 0, Element 2', 'Process 0, Element 3', 'Process 0, Element 4', 
'Process 4, Element 0', 'Process 4, Element 1', 'Process 4, Element 2',
'Process 4, Element 3', 'Process 4, Element 4', 'Process 3, Element 0', 
'Process 3, Element 1', 'Process 3, Element 2', 'Process 3, Element 3', 
'Process 3, Element 4']

Outside of IF
['Process 2, Element 0','Process 2, Element 1', 'Process 2, Element 2', 
'Process 2, Element 3', 'Process 2, Element 4', 'Process 1, Element 0', 
'Process 1, Element 1', 'Process 1, Element 2', 'Process 1, Element 3', 
'Process 1, Element 4', 'Process 0, Element 0', 'Process 0, Element 1', 
'Process 0, Element 2', 'Process 0, Element 3', 'Process 0, Element 4', 
'Process 4, Element 0', 'Process 4, Element 1', 'Process 4, Element 2', 
'Process 4, Element 3', 'Process 4, Element 4', 'Process 3, Element 0', 
'Process 3, Element 1', 'Process 3, Element 2', 'Process 3, Element 3', 
'Process 3, Element 4']
Run Code Online (Sandbox Code Playgroud)