+1 vote
in Programming Languages by (73.2k points)

I think the multiprocessing module of Python does not allow to pass multiple arguments to Pool.map().  

E.g.

from multiprocessing import Pool

def f(x):

    return x*x

if __name__ == '__main__':

    with Pool(5) as p:

        print(p.map(f, [1, 2, 3]))

Is there any way to pass multiple arguments? 

1 Answer

+1 vote
by (346k points)
selected by
 
Best answer

You can use Pool.starmap() instead of Pool.map() to pass multiple arguments. The function is as follows:

starmap(func, iterable[, chunksize])

Here is an example that uses starmap().

from multiprocessing import Pool
import numpy as np

def func(x, y):
    return x + y

a = [11, 12, 13, 14, 15, 16, 17]
b = [1, 2, 3, 4, 5, 6, 7]
with Pool(2) as pool:
    v = pool.starmap(func, [(a[j], b[j]) for j in range(len(a))])
print(v)

In the above example, I am passing 2 arguments, a[j] and b[j], to the function func(). The output of the code is the following:

[12, 14, 16, 18, 20, 22, 24]


...