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()?
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:
Multiply a sequence:
for res in pool.map(worker, [1,2,3], [False] * 3):
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With