Peter Smit Peter Smit - 2 years ago 188
Python Question

How to let take a lambda function

I the following function

def copy_file(source_file, target_dir):

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

p = new Pool(12) 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?

Answer Source

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, file_list)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download