3

I'm following the answer on Is there a simple process-based parallel map for python? using Python 3.6.1. I'm trying to use it the same exact way as I would use the regular map function. Why does this happen? How can I create a parallel map function for use with lambda?

def map_parallel(func, iterable, n_jobs=-1):
    if n_jobs == -1:
        n_jobs = multiprocessing.cpu_count()
    pool = multiprocessing.Pool(processes=n_jobs)
    return pool.map(func, iterable)

map_parallel(lambda x: x + 3, range(100))


PicklingError: Can't pickle <function <lambda> at 0x185284f28>: attribute lookup <lambda> on __main__ failed

1 Answer 1

0

multiprocessing pickle objects to pass between processes. lambda is not serializable (See What can be pickled and unpickled?); it can't be passed between processes.

You need to define a function, and pass that.

def add3(n):
    return n + 3

map_parallel(add3, range(100))

Or for this case, you can use functools.partial with operator.add:

import functools
import operator

map_parallel(functools.partial(operator.add, 3), range(100))
Sign up to request clarification or add additional context in comments.

2 Comments

Is it possible to convert lambda to a function? I rarely ever use map with functions and use it on the fly with transforming iterables
@O.rka, Check this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.