In the following code, I want to multprocess sum_
for three different values of z
which are included in np.array([1,2,3])
:
from multiprocessing import Pool
from functools import partial
import numpy as np
def sum_(x, y, z):
return x**1+y**2+z**3
sum_partial = partial(sum_, x = 1, y = 2) # freeze x and y
a = np.array([1,2,3]) # three different values for z
p = Pool(4)
p.map(sum_partial, a)
p.map(sum_partial, a)
gives the following error: TypeError: sum_() got multiple values for keyword argument 'x'
, because for Python I reassign a
to the kwarg x
of my function. How can I make each variable of np.array([1,2,3])
to fill the argumentz
of sum_
instead of x
so that I can get the following result:
[6, 13, 32]
which are respectively:
sum_partial(z=1), sum_partial(z=2), sum_partial(z=3)
?
I would like to keep using pool.map
.
Btw is that possible to use multiprocessing both with an array of y
and an array of z
to finally get a list of len(y)*len(z)
values?