I have the following code.
from itertools import repeat
from multiprocessing import Pool
import os
import numpy as np
a=[1,2,3]
b=[2,3,4]
def multi_run_wrapper(args):
return add(*args)
def add(x,y):
return x+y
if __name__ == "__main__":
pool = Pool( os.cpu_count())
results = pool.starmap(add,zip(a,b))
print(sum(results))
the output would be results= [3, 5, 7]
. and print out 15
.
So in this way, if I want to calculate sum of results
, I need to save the whole list results
.
Is there a way of using multiprocessing without saving the whole list? For example, sum the results
while using mulprocessing. Because if my list a
and b
become super long, then the output results
would take too much memory and it won't fit in my laptop.
In other words, my goal is to get sum(results)
without saving the whole list but speed up the process at the same time.
Thank you