Search code examples
pythondictionarymultiprocessingargumentspool

python Pool map multiple arguments - list and variable as input


I would like to multiprocess a function (see below)

def func(c):
    time =  time
    list = [c, time]
    another_func(list)

I run this above function as follows:

p = multiprocess.Pool()
p.map(func, cust, chunksize=1)
p.close()
p.join()

cust is a list of strings like

cust = ['c1', 'c2', ...]

Now my question is it possible to get the time variable into the p.map like

p.map(func, cust, time, chunksize=1) 

I have searched multiple topics here but I did not find a matching topic.

Thx for any help/hints!


Solution

  • you can use starmap:

    def func(c, time):
        my_list = [c, time]
        another_func(my_list)
    
    
    p.starmap(func, [(c, time) for c in cust], chunksize=1) 
    

    even better:

    p.map(another_func, [[c, time] for c in cust], chunksize=1)