I am trying to pass a list as a parameter to the pool.map(co_refresh, input_list)
. However, pool.map
didn\'t trigger the function co_refresh
georgexsh's answer works perfectly in Python 3; the key is that starmap
allows to pass multiple arguments into the function.
However, if you use Python 2, you will need to use python classical unpacking mentioned in comments by Ahmed under the question here.
In my case, I just need to "enlist" the argument first in the function.
def func(args)
(a, b, c, d) = args
# You can then use a, b, c, d in your function
return;
Consider the below code
from multiprocessing.pool import Pool
data = [["a1", "b1", "c1", "d1"],
["a2", "b2", "c2", "d2"],
["a3", "b3", "c3", "d3"], ]
def someaction(a, b=1, c=2, d=3):
print(a, b, c, d)
When you call this in your script using a pool
pool = Pool(4)
pool.map(someaction, data)
The output is
['a1', 'b1', 'c1', 'd1'] 1 2 3
['a2', 'b2', 'c2', 'd2'] 1 2 3
['a3', 'b3', 'c3', 'd3'] 1 2 3
So a
gets the array and rest all parameters are not passed. Pool.map
expects a function to only have one argument. So for your case to work you need to create a wrapper function
def someaction_wrapper(data):
someaction(*data)
And then call this wrapper function in pool. Now you use
pool = Pool(4)
pool.map(someaction_wrapper, data)
And the output is
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
Which is what you wanted I believe
You should define your work function before declaring the Pool
, when you declaring Pool
, sub worker processes forked from that point, worker process don't execute code beyond that line, therefore not seeing your work function.
Besides, you'd better replace pool.map
with pool.starmap
to fit your input.
A simplified example:
from multiprocessing import Pool
def co_refresh(a, b, c, d):
print(a, b, c, d)
input_list = [f'a{i} b{i} c{i} d{i}'.split() for i in range(4)]
# [['a0', 'b0', 'c0', 'd0'], ['a1', 'b1', 'c1', 'd1'], ['a2', 'b2', 'c2', 'd2'], ['a3', 'b3', 'c3', 'd3']]
pool = Pool(processes=3)
pool.starmap(co_refresh, input_list)
pool.close()