Skip to content
Advertisement

Python: How to pass output of the map function as arguments

I need to pass some arguments to the starmap_async function. I have a list my_list that has different values in it and some constants that are repeated. I have a way to pass the arguments as bellow:

import multiprocessing as mp
from itertools import repeat

def f(a,b,c,d):
    return (a,b,c,d)

my_list = [1,2,3,4,5]

with mp.Pool(cpu_num) as pool:
    res = pool.starmap_async(f, zip(my_list, repeat(a),repeat(b),repeat(c),repeat(d)))
    pool.close()
    pool.join()
    res.get()

It works and generates arguments like this which is the desired output:

(1,a,b,c,d)
(2,a,b,c,d)
(3,a,b,c,d)
(4,a,b,c,d)
(5,a,b,c,d)

Now I want to use the map function to do something like this:

res = pool.starmap_async(f, zip(my_list, repeat(list(map(repeat, 
[a, b, c, d])))))

but then I end up with arguments like this:

(1,(a,b,c,d))
(2,(a,b,c,d))
(3,(a,b,c,d))
(4,(a,b,c,d))
(5,(a,b,c,d))

How can I fix that issue?

Advertisement

Answer

Try this:

import multiprocessing as mp
from itertools import repeat

def f(a,b,c,d):
    return (a,b,c,d)

my_list = [1,2,3,4,5]

with mp.Pool(cpu_num) as pool:
    res = pool.starmap_async(f, zip(my_list, *map(repeat, (a,b,c,d))))
    pool.close()
    pool.join()
    res.get()
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement