我想使用Python multiprocessing
来加速我的程序,我的代码工作:
from multiprocessing import Process
class MyProcess(Process):
def __init__(self,name, array):
super(MyProcess,self).__init__()
self.name = name
self.array = array
def run(self):
s = 0
for a in self.array:
s += a
self.s = s
if __name__ == '__main__':
process_list = []
for i in range(5):
p = MyProcess(str(i), [1, 2, 3, 4, 5])
p.start()
process_list.append(p)
for p in process_list:
p.join()
# for p in process_list:
# print(p.s)
在这个示例代码中,我想计算输入数组的和。如何获得计算结果?
print(p.s)
报告错误:MyProcess
对象没有属性"s"。
任何建议都会被采纳~~~
您可以从concurrent.futures
使用ProcessPoolExecutor
并收集结果:
from concurrent.futures import ProcessPoolExecutor
input_list = [1, 2, 3, 4, 5]
def calculate_sum(a: list):
return sum(a)
if __name__ == "__main__":
future_obj = ProcessPoolExecutor().submit(calculate_sum, input_list)
print(future_obj.result())
感谢每一个有用的答案。这篇文章对我帮助很大。然而,该帖子提供了许多答案来收集函数子流程而不是类子流程的答案。
我尝试了一种解决方案来收集类子流程的答案,代码是:
import multiprocessing
from multiprocessing import Process
import numpy as np
class MyProcess(Process):
def __init__(self,name, array):
super(MyProcess,self).__init__()
self.name = name
self.array = array
recv_end, send_end = multiprocessing.Pipe(False)
self.recv = recv_end
self.send = send_end
def run(self):
s = 0
for a in self.array:
s += a
self.send.send(s)
def getResult(self):
return self.recv.recv()
if __name__ == '__main__':
process_list = []
for i in range(5):
a = np.random.random(10)
print(i, ' correct result: ', a.sum())
p = MyProcess(str(i), a)
p.start()
process_list.append(p)
for p in process_list:
p.join()
for p in process_list:
print(p.name, ' subprocess result: ', p.getResult())