Reputation: 35
I can't find an appropriate method for the next task: I have an input massive [x1, x2, x3 ... xn] and function that I want to apply to every x from massive f(xi, param) so there are eight possible values of this param, and the result of function doesn't depend from param. So I need to run this function f for all possible values of param at the same time. and it's absolutely not important which function work with which x. it's important to all this eight function f(xi, p1), f(xi, p2)... f(xi, p8) works at the same time and all the x will be processed only once.
Upvotes: 3
Views: 141
Reputation: 134
Seems pretty straightforward. I don't know what you mean by "all the x will be processed only once", but that doesn't sound possible. The function still has to be called separately for each parameter (Unless some black magician on SO can prove me wrong, which seems likely)
from multiprocessing import Pool
def f(x, param):
...do stuff...
if __name__ == '__main__':
params = [1,2,3,4,5,6,7,8]
try:
pool = Pool(8)
pool.starmap(f, [(x,param) for param in params])
except SomeError:
...do error stuff...
finally:
pool.close()
pool.join()
Upvotes: 1