11

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. And also no error returned. It looks like the process hung in there.

Original Code:

from multiprocessing import Pool
import pandas as pd
import os

account='xxx'
password='xxx'
threads=5
co_links='file.csv'

input_list=[]

pool = Pool(processes=threads)
def co_refresh(url, account, password, outputfile):

    print(url + ' : ' + account + ' : ' + password + ' : ' + outputfile)

    return;

link_pool = pd.read_csv(co_links, skipinitialspace = True)

for i, row in link_pool.iterrows():

    ln = (row.URL, account, password, os.path.join('e:/', row.File_Name.split('.')[0] + '.csv'))

    input_list.append(ln)

pool.map(co_refresh, input_list)

pool.close()

However, it never triggered the function co_refresh. How can I use the list as a parameter to be passed to my function?

Old Question (Simplified):

I have below input_list, which is a list of list:

[a1, b1, c1, d1]
[a2, b2, c2, d2]
[a3, b3, c3, d3]

I have the function as below:

def func(a, b, c, d)
   ###
    return;

I would like to use multiprocess for this function func:

from multiprocessing import Pool
pool = Pool(processes=5)
pool.map(func, input_list)
pool.close()

However, it never triggered the function func. How can I use the list as a parameter to be passed to my function?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
lovechillcool
  • 762
  • 2
  • 10
  • 32
  • This is not exactly the code you tested, is it? It is full of errors. Post exactly what you tried and then you'll get an exact answer. – zvone Nov 21 '17 at 23:50
  • As @zvone suggested, you should share with us the exact code you have tested and the error reported by `Python` interpreter. Currently the pieces you give doesn't compose a runnable script. – Patrick the Cat Nov 21 '17 at 23:54
  • I have updated the question. @Mai, can you please help? – lovechillcool Nov 22 '17 at 07:49
  • On the top level of the script, you should only define functions and classes, not actally execute any of it. The code to be executed should be called from withing a `if __name__ == "__main__"` block. That is in general a good idea, but in case of multiprocessing, it is essential. Multiprocessing will import your module again in another process. The way it is, that will cause all of the coe to be executed again, rather than what you'd expect. – zvone Nov 22 '17 at 22:48

3 Answers3

17

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()
georgexsh
  • 15,984
  • 2
  • 37
  • 62
  • I finally got time to test this... I now understand where the issue is... However, the process completed but didn't print anything. Do you know why is that? – lovechillcool Dec 11 '17 at 17:30
  • @lovechillcool are you using the code I posted? note that it is for python3.6. – georgexsh Dec 12 '17 at 05:29
4

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

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
0

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;
lovechillcool
  • 762
  • 2
  • 10
  • 32