我有下面的代码,我想在并行处理完成后如何获得返回值。我不喜欢对getdata函数做任何更改。
from multiprocessing import Process
class Calculation(object):
def __init__(self, a, b, c):
self.A, self.B, self.C= a, b, c
def getdata(self):
self.Product = self.A * self.B
self.Subtract = self.C - self.B
self.Addition = self.A + self.B + self.C
return self.Product, self.Subtract, self.Addition
def foo():
EXTERNAL_C=[10, 20, 30, 40, 50, 20, 40]
c = [Calculation(a=4, b=5, c=n) for n in EXTERNAL_C]
return c
K = []
for M in foo():
p = Process(target=M.getdata)
p.start()
K.append(p)
for process in K:
process.join()
print(K)输出:
[<Process(Process-8, stopped[1])>, <Process(Process-9, stopped[1])>, <Process(Process-10, stopped[1])>, <Process(Process-11, stopped[1])>, <Process(Process-12, stopped[1])>, <Process(Process-13, stopped[1])>, <Process(Process-14, stopped[1])>]发布于 2021-01-07 10:22:54
您在主进程中创建的Calculation对象被复制到生成的进程中,因此存在没有办法提取他们的状态或获得getdata的返回值,而不进行显式操作。
您可以使用multiprocessing.Queue来存储结果,如下所示
from multiprocessing import Process, Queue
class Calculation(object):
def __init__(self, a, b, c):
self.A, self.B, self.C = a, b, c
def getdata(self, id, queue):
self.Product = self.A * self.B
self.Subtract = self.C - self.B
self.Addition = self.A + self.B + self.C
queue.put(
{
"id": id,
"Product": self.Product,
"Subtract": self.Subtract,
"Addition": self.Addition,
}
)
def foo():
EXTERNAL_C = [10, 20, 30, 40, 50, 20, 40]
c = [Calculation(a=4, b=5, c=n) for n in EXTERNAL_C]
return c
K = []
f = foo()
queue = Queue()
for id, M in enumerate(f):
p = Process(target=M.getdata, args=(id, queue))
p.start()
K.append(p)
for process in K:
process.join()
results = [queue.get() for i in range(len(f))]
print(results)https://stackoverflow.com/questions/65610002
复制相似问题