ThreadPoolExecutor map method with multiple parameters
ThreadPoolExeuctor from concurrent.futures package in Python 3 is very useful for executing a task (function) with a set of data (parameter) concurrently and this post lists examples on how to pass MULTIPLE parameters to the task being executed.
Pass by same length iterables
from concurrent.futures import ThreadPoolExecutor
import threading
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
prefix = "test_"
def add_prefix(prefix, i):
print("%s: %s%s" % (threading.get_ident(), prefix, i))
if __name__ == "__main__":
with ThreadPoolExecutor(10) as pool:
pool.map(add_prefix, [prefix] * len(data), data)
Pass with wrapper lambda function
from concurrent.futures import ThreadPoolExecutor
import threading
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
prefix = "test_"
def add_prefix(prefix, i):
print("%s: %s%s" % (threading.get_ident(), prefix, i))
if __name__ == "__main__":
with ThreadPoolExecutor(10) as pool:
args = ((prefix, i) for i in data)
pool.map(lambda p: add_prefix(*p), args)
Pass with repeat function from itertools
from concurrent.futures import ThreadPoolExecutor
import threading
from itertools import repeat
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
prefix = "test_"
def add_prefix(prefix, i):
print("%s: %s%s" % (threading.get_ident(), prefix, i))
if __name__ == "__main__":
with ThreadPoolExecutor(10) as pool:
pool.map(add_prefix, repeat(prefix), data)
References
- concurrent.futures – Launching parallel tasks
- Pass multiple parameters to concurrent.futures.Executor.map?