I want to use Python multiprocessing
to accelerate my program, and my code work:
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)
In this example code, I want to calculate the sum of the input array. How can I obtain the calculated result?
print(p.s)
reports bug: MyProcess
object has no attribute 's'.
Any suggestion is apprecated~~~