Greg
Greg

Reputation: 8915

How to multiprocess functions

How can non-iterable kwargs be included in the map function of concurrent.futures? For example, in the code below, how can c be called as False for all function calls of single? That is, single(iterable_a[i], iterable_b[i], c=False) where i is an int between 0 and 3.

import concurrent.futures

def single(a, b, c=True):
    return (a, b, c)

if __name__ == '__main__':
    iterable_a = range(4)
    iterable_b = range(4,8)
    with concurrent.futures.ProcessPoolExecutor() as executor:
        results = executor.map(single, iterable_a, iterable_b)

Upvotes: 1

Views: 107

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476584

You can use a functools.partial for that:

from functools import partial

single2 = partial(single,c=False)

will generate a function single2 where c is set to False. So you can use:

from functools import partial

results = executor.map(partial(single,c=False), iterable_a, iterable_b)

So your map calls a function generated from partial(single,c=False) and you map with that function.

Upvotes: 3

Related Questions