具有多個參數的 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