来自https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.Executor.map
如果 func 调用引发异常,则在从迭代器检索其值时将引发该异常。
以下代码段仅输出第一个例外 (Exeption: 1),然后停止。这是否与上述说法相矛盾?我希望以下内容打印出循环中的所有异常。
def test_func(val):
raise Exception(val)
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
for r in executor.map(test_func,[1,2,3,4,5]):
try:
print r
except Exception as exc:
print 'generated an exception: %s' % (exc)
Run Code Online (Sandbox Code Playgroud)