python - Make the function within pool.map to act on one specific argument of its multiple arguments -
in following code, want multprocess sum_ 3 different values of z included in np.array([1,2,3]):
from multiprocessing import pool functools import partial import numpy np def sum_(x, y, z): return x**1+y**2+z**3 sum_partial = partial(sum_, x = 1, y = 2) # freeze x , y = np.array([1,2,3]) # 3 different values z p = pool(4) p.map(sum_partial, a) p.map(sum_partial, a) gives following error: typeerror: sum_() got multiple values keyword argument 'x', because python reassign a kwarg x of function. how can make each variable of np.array([1,2,3]) fill argumentz of sum_ instead of x can following result:
[6, 13, 32] which respectively:
sum_partial(z=1), sum_partial(z=2), sum_partial(z=3) ? keep using pool.map.
btw possible use multiprocessing both array of y , array of z list of len(y)*len(z) values?
i find answer here
in case it'll be:
import multiprocessing mp def sum_(x, y, z): return x**1+y**2+z**3 def mf_wrap(args): return sum_(*args) p = mp.pool(4) = [1,2,3] b = [0.1,0.2,0.3] fl = [(1, i, j) in j in b] #mf_wrap = lambda args: myfun(*args) -> sucker, though more pythonic , compact, won't work p.map(mf_wrap, fl)
Comments
Post a Comment