简体   繁体   English

多处理获取数组

[英]Multiprocessing obtaining array

I want to get the result_1 and result_2 arrays with the following code:我想使用以下代码获取 result_1 和 result_2 arrays:

import multiprocessing as mp
import numpy as np

result_1=[]
result_2=[]

a=np.random.rand(10,10)
b=np.random.rand(7,7)

def inv_1(x):
    result_1.append(np.linalg.inv(x))

def inv_2(y):
    result_2.append(np.linalg.inv(y))


if __name__ == "__main__":

    p1 = mp.Process(target=inv_1, args=(a),)
    p2 = mp.Process(target=inv_2, args=(b),)

    p1.start()
    p2.start()

    p1.join()
    p2.join()

    print(result_1, result_2)

However, when I run the code, I get the following output:但是,当我运行代码时,我得到以下 output:

[] []

How can I solve this problem?我怎么解决这个问题?

Unlike threads, you can't share arbitrary variables between processes.与线程不同,您不能在进程之间共享任意变量。 To do what you're trying to do, you can create shared lists using a multiprocessing.Manager object, eg:要执行您想执行的操作,您可以使用multiprocessing.Manager object 创建共享列表,例如:

import multiprocessing as mp
import numpy as np

a=np.random.rand(10,10)
b=np.random.rand(7,7)

def inv_1(x, target):
    target.append(np.linalg.inv(x))

def inv_2(y, target):
    target.append(np.linalg.inv(y))

if __name__ == "__main__":
    mgr = mp.Manager()

    result_1 = mgr.list()
    result_2 = mgr.list()

    q = mp.Queue()

    p1 = mp.Process(target=inv_1, args=(a, result_1),)
    p2 = mp.Process(target=inv_2, args=(b, result_2),)

    p1.start()
    p2.start()

    p1.join()
    p2.join()

    print('RESULT 1:', result_1)
    print('RESULT 2:', result_2)

This does what you're trying to do, although it's not clear to me why you're doing it this way -- both result_1 and result_2 only have a single value (because you're just appending an item to an empty list), so it's not clear why you need a list in the first place.这就是你想要做的,虽然我不清楚你为什么这样做 - result_1result_2只有一个值(因为你只是将一个项目附加到一个空列表),所以不清楚为什么你首先需要一个列表。


More broadly, you might want to redesign your code so that it doesn't rely on shared variables.更广泛地说,您可能希望重新设计代码,使其不依赖于共享变量。 A common solution is to use a queue to pass data from your workers back to the main process.一个常见的解决方案是使用队列将数据从您的工作人员传递回主进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM