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:
JavaScript
x
14
14
1
import multiprocessing as mp
2
from itertools import repeat
3
4
def f(a,b,c,d):
5
return (a,b,c,d)
6
7
my_list = [1,2,3,4,5]
8
9
with mp.Pool(cpu_num) as pool:
10
res = pool.starmap_async(f, zip(my_list, repeat(a),repeat(b),repeat(c),repeat(d)))
11
pool.close()
12
pool.join()
13
res.get()
14
It works and generates arguments like this which is the desired output:
JavaScript
1
6
1
(1,a,b,c,d)
2
(2,a,b,c,d)
3
(3,a,b,c,d)
4
(4,a,b,c,d)
5
(5,a,b,c,d)
6
Now I want to use the map
function to do something like this:
JavaScript
1
3
1
res = pool.starmap_async(f, zip(my_list, repeat(list(map(repeat,
2
[a, b, c, d])))))
3
but then I end up with arguments like this:
JavaScript
1
6
1
(1,(a,b,c,d))
2
(2,(a,b,c,d))
3
(3,(a,b,c,d))
4
(4,(a,b,c,d))
5
(5,(a,b,c,d))
6
How can I fix that issue?
Advertisement
Answer
Try this:
JavaScript
1
14
14
1
import multiprocessing as mp
2
from itertools import repeat
3
4
def f(a,b,c,d):
5
return (a,b,c,d)
6
7
my_list = [1,2,3,4,5]
8
9
with mp.Pool(cpu_num) as pool:
10
res = pool.starmap_async(f, zip(my_list, *map(repeat, (a,b,c,d))))
11
pool.close()
12
pool.join()
13
res.get()
14