Executor.map 未处理并发期货

Executor.map on concurrent futures is not processed

我不明白为什么一个简单的执行器不能工作

我有两个模块:main.py

import other

a = [1, 2, 3]
b = ['a', 'b'] 
l = ['a', 'b', 'd']

other.run_executor(a, b, l)

other.py

from concurrent import futures
from multiprocessing import cpu_count


def do_stuff(a, b, c):
    print(a, b, c)


def run_executor(a, b, l):
    iterat = ((a, b, c) for c in l)
    with futures.ThreadPoolExecutor(max_workers=cpu_count() - 1) as e:
        e.map(do_stuff, iterat)

运行 此代码没有引发错误,但也没有打印任何内容。 它应该打印 a, b, l[0]; a, b, l[1];一个,乙,l[2] 我做错了什么?

这是因为您没有正确使用ThreadPoolExecutor.map。参数不应作为一个迭代器传递,而应作为 3 个迭代器传递:

from concurrent import futures
from itertools import repeat
from multiprocessing import cpu_count


def do_stuff(a, b, c):
    print(a, b, c)


def run_executor(a, b, l):
    with futures.ThreadPoolExecutor(max_workers=cpu_count() - 1) as e:
        e.map(do_stuff, repeat(a), repeat(b), c)

此外,您应该在主脚本中使用 if __name__ == "__main__" 保护以避免奇怪的行为。如果您尝试 import/pickle 其中定义的函数,此许可可以保护您的模块 main 不被执行。

import other

if __name__ == "__main__":
    a = [1, 2, 3]
    b = ['a', 'b'] 
    l = ['a', 'b', 'd']

    other.run_executor(a, b, l)