Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to let Pool.map take a lambda function

I have the following function:

def copy_file(source_file, target_dir):     pass 

Now I would like to use multiprocessing to execute this function at once:

p = Pool(12) p.map(lambda x: copy_file(x,target_dir), file_list) 

The problem is, lambda's can't be pickled, so this fails. What is the most neat (pythonic) way to fix this?

like image 317
Peter Smit Avatar asked Jan 28 '11 10:01

Peter Smit


People also ask

How do you use lambda and map in Python?

To work with map(), the lambda should have one parameter in, representing one element from the source list. Choose a suitable name for the parameter, like n for a list of numbers, s for a list of strings. The result of map() is an "iterable" map object which mostly works like a list, but it does not print.

Is map a lambda function?

The map() function in Python takes in a function and a list as an argument. The function is called with a lambda function and a list and a new list is returned which contains all the lambda modified items returned by that function for each item.

What is the correct way to use a lambda function?

Syntax. Simply put, a lambda function is just like any normal python function, except that it has no name when defining it, and it is contained in one line of code. A lambda function evaluates an expression for a given argument. You give the function a value (argument) and then provide the operation (expression).

How does pool map work Python?

The pool's map method chops the given iterable into a number of chunks which it submits to the process pool as separate tasks. The pool's map is a parallel equivalent of the built-in map method. The map blocks the main execution until all computations finish. The Pool can take the number of processes as a parameter.


1 Answers

Use a function object:

class Copier(object):     def __init__(self, tgtdir):         self.target_dir = tgtdir     def __call__(self, src):         copy_file(src, self.target_dir) 

To run your Pool.map:

p.map(Copier(target_dir), file_list) 
like image 62
Fred Foo Avatar answered Sep 27 '22 20:09

Fred Foo