Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing multiple parameters in ThreadPoolExecutor.map()

The following code:

import concurrent.futures 

def worker(item, truefalse):
    print(item, truefalse)
    return item

processed = []
with concurrent.futures.ThreadPoolExecutor() as pool:
    for res in pool.map(worker, [1,2,3], False):
        processed.append(res)

Yields an exception: TypeError: zip argument #2 must support iteration

I also tried: for res in pool.map(worker, ([1,2,3], False)):

Which yields: TypeError: worker() missing 1 required positional argument: 'truefalse'

How does one pass multiple arguments to the function in calling ThreadPoolExecutor.map()?

like image 975
Wells Avatar asked Mar 07 '26 11:03

Wells


1 Answers

If you're trying to call the worker function with 1, False, then 2, False, then 3, False, you need to extend your single False to an iterable of Falses at least as long as the other iterable. Two approaches that work:

  1. Multiply a sequence:

    for res in pool.map(worker, [1,2,3], [False] * 3):
    
  2. Use itertools.repeat to make it as long as needed (map will stop when the shortest iterable is exhausted). Add from itertools import repeat to the top of the file, then use:

    for res in pool.map(worker, [1,2,3], repeat(False)):
    

For the record, this is also how you'd do this with regular map, not just ThreadPoolExecutor.

like image 93
ShadowRanger Avatar answered Mar 08 '26 23:03

ShadowRanger