具有多个参数的 ThreadPoolExecutor map 方法

Python 3 中 concurrent.futures 包中的 ThreadPoolExecutor 对于同时执行具有一组数据(参数)的任务(函数)非常有用,这篇文章列出了如何将多个参数传递给正在执行的任务的示例。

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)

参考资料

concurrent multithreading python