Steven Chan
Steven Chan

Reputation: 473

Pass multiple arguments and iterables to Python's multiprocessing.Pool.map

I've read many posts regarding to passing multiple arguments or passing iterables to Python's multiprocessing.Pool.map, but none of these seems to discuss the most general case, i.e, passing multiple arguments AND multiple iterables. In code, it should look like:

import multiprocessing as mp

with mp.Pool() as pool:
    pool.map(func, args=(), iterables=()) # passing multiple args and iterables

How can I achieve this?

Upvotes: 2

Views: 1604

Answers (1)

FMc
FMc

Reputation: 42411

When using Pool.map() and related functions, your worker function must take a single value: one item of work. If an item of work must contain multiple values, create an appropriate data structure to hold them (e.g. tuple, list, dict, or instance of a user-defined class).

Similarly, Pool.map() processes a single iterable. However, iterables can be chained, zipped, or combined in various ways. See the itertools package for some examples.

Upvotes: 1

Related Questions