Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing multiple parameters to pool.map() function in Python [duplicate]

I need some way to use a function within pool.map() that accepts more than one parameter. As per my understanding, the target function of pool.map() can only have one iterable as a parameter but is there a way that I can pass other parameters in as well? In this case, I need to pass in a few configuration variables, like my Lock() and logging information to the target function.

I have tried to do some research and I think that I may be able to use partial functions to get it to work? However I don't fully understand how these work. Any help would be greatly appreciated! Here is a simple example of what I want to do:

def target(items, lock):     for item in items:         # Do cool stuff         if (... some condition here ...):             lock.acquire()             # Write to stdout or logfile, etc.             lock.release()  def main():     iterable = [1, 2, 3, 4, 5]     pool = multiprocessing.Pool()     pool.map(target(PASS PARAMS HERE), iterable)     pool.close()     pool.join() 
like image 862
DJMcCarthy12 Avatar asked Aug 28 '14 16:08

DJMcCarthy12


People also ask

How do you pass multiple arguments in multiprocessing in Python?

Passing Keyword Arguments to Multiprocessing Processes We can also pass in arguments corresponding to the parameter name using the kwargs parameter in the Process class. Instead of passing a tuple, we pass a dictionary to kwargs where we specify the argument name and the variable being passed in as that argument.

What does pool starmap return?

We can then call the starmap() function on the process pool to apply our task() function to each tuple of arguments in the prepared list. This returns an iterator over the results returned from the task() function, in the order that function calls are completed.

What is Pool map in Python?

It supports asynchronous results with timeouts and callbacks and has a parallel map implementation. — multiprocessing — Process-based parallelism. The built-in map() function allows you to apply a function to each item in an iterable. The Python process pool provides a parallel version of the map() function.


1 Answers

You can use functools.partial for this (as you suspected):

from functools import partial  def target(lock, iterable_item):     for item in iterable_item:         # Do cool stuff         if (... some condition here ...):             lock.acquire()             # Write to stdout or logfile, etc.             lock.release()  def main():     iterable = [1, 2, 3, 4, 5]     pool = multiprocessing.Pool()     l = multiprocessing.Lock()     func = partial(target, l)     pool.map(func, iterable)     pool.close()     pool.join() 

Example:

def f(a, b, c):     print("{} {} {}".format(a, b, c))  def main():     iterable = [1, 2, 3, 4, 5]     pool = multiprocessing.Pool()     a = "hi"     b = "there"     func = partial(f, a, b)     pool.map(func, iterable)     pool.close()     pool.join()  if __name__ == "__main__":     main() 

Output:

hi there 1 hi there 2 hi there 3 hi there 4 hi there 5 
like image 148
dano Avatar answered Oct 14 '22 06:10

dano