使用带有地图的 ThreadPoolExecutor 时出错

get error using ThreadPoolExecutor with map

我注意到当函数 (x) 被 ThreadPoolExecutor() 调用时,我的代码没有输出错误或中断:

import concurrent.futures

def function(x):
   # do sth invalid such as
   x = y + 1 #there is no y variable

input_list = [1,2,3]
with concurrent.futures.ThreadPoolExecutor() as executor: 
    executor.map(function,input_list)  

如何使用 map() 而不是 executor 的 submit() 获取错误输出?

executer.map returns 一个 iterable 必须迭代以获得单独的结果,包括可能抛出的任何异常:

import concurrent.futures

def function(x):
    # do sth invalid such as
    if x == 2:
        raise ValueError("I don't like 2")
    return x, x ** 2

input_list = [1,2,3]
with concurrent.futures.ThreadPoolExecutor(3) as executor:
    results = executor.map(function,input_list)
    try:
        for x, return_value in results:
            print(f'{x} ** 2 = {return_value}')
    except Exception as e:
        print(e)

打印:

1 ** 2 = 1
I don't like 2

或者您可以稍微不同地迭代并获得相同的结果:

import concurrent.futures

def function(x):
    # do sth invalid such as
    if x == 2:
        raise ValueError("I don't like 2")
    return x, x ** 2

input_list = [1,2,3]
with concurrent.futures.ThreadPoolExecutor(3) as executor:
    results = executor.map(function,input_list)
    while True:
        try:
            x, return_value = next(results)
        except StopIteration:
            break
        except Exception as e:
            print(e)
        else:
            print(f'{x} ** 2 = {return_value}')

但是,如果你使用包multiprocessing.pool中的classThreadPool,那么你可以得到全部的结果,也就是得到结果除了可能抛出的任何异常(使用 imap):

from multiprocessing.pool import ThreadPool

def function(x):
    # do sth invalid such as
    if x == 2:
        raise ValueError("I don't like 2")
    return x, x ** 2

input_list = [1,2,3]
with ThreadPool(3) as executor:
    results = executor.imap(function,input_list)
    while True:
        try:
            x, return_value = next(results)
        except StopIteration:
            break
        except Exception as e:
            print(e)
        else:
            print(f'{x} ** 2 = {return_value}')

打印:

1 ** 2 = 1
I don't like 2
3 ** 2 = 9