Matt Joiner Matt Joiner - 3 months ago 57
Python Question

Pass multiple parameters to concurrent.futures.Executor.map?

The

takes a variable number of iterables from which the function given is called. How should I call it if I have a generator that produces tuples that are normally unpacked in place?

The following doesn't work because the each of the generated tuples is given as a different argument to map:

args = ((a, b) for (a, b) in c)
for result in executor.map(f, *args):
pass


Without the generator, the desired arguments to map might look like this:

executor.map(
f,
(i[0] for i in args),
(i[1] for i in args),
...,
(i[N] for i in args),
)

vz0 vz0
Answer

You need to remove the * on the map call:

args = (a, b for b in c)
for result in executor.map(f, args):
    pass

This will call f, len(args) times, where f should accept one parameter. If you want f to accept two parameters you can use a lambda call like:

args = (a, b for b in c)
for result in executor.map(lambda p: f(*p), args):
    pass
Comments