I have a list of functions that I want to execute parallely
list1 = [a,b,c]
List b contains a mapping of arguments to be passed to each function respectively. i,e
list2 = [d,e,f]
So function a takes argument d, function b takes argument e and so on.
I am creating a tuple of function and args using below
tasks = list(zip(list1,list2))
running the parallel function call below
futures = [pool.apply_async(*t) for t in tasks]
Now I have another list c containing variables to store the result for each of the function.
list3 = [g,h,i]
Therefore result of function a taking argument d should be stored in list3[0], result of function b taking argument e should be stored in list3[1] and so on. How can I achieve this ?
CodePudding user response:
Well, your futures
are in that order already, so
list3 = [fut.get() for fut in futures]
will wait for them to be ready, and store the results in that list:
import multiprocessing
def fun1(arg):
return f"fun1! {arg}???"
def fun2(arg):
return f"fun2? {arg}!"
def fun3(arg):
return f"fun3: {arg}..."
def main():
funcs = [fun1, fun2, fun3]
argses = [("hey",), ("ho",), ("let's go",)]
with multiprocessing.Pool() as pool:
futures = [pool.apply_async(*t) for t in zip(funcs, argses)]
results = [f.get() for f in futures]
print(results)
if __name__ == '__main__':
main()
prints out
['fun1! hey???', 'fun2? ho!', "fun3: let's go..."]