来自https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.Executor.map
如果func调用引发异常,那么当从迭代器中检索其值时,将引发该异常。
以下代码段仅输出第一个异常(Exception: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)
如上所述,遗憾的是executor.map的API是有限的,只允许您获得第一个异常。此外,在遍历结果时,您将只获得第一个异常的值。
要回答您的问题,如果您不想使用其他库,则可以展开地图并手动应用每个功能:
future_list = []
with concurrent.futures.ThreadPoolExecutor() as executor:
for arg in range(10):
future = executor.submit(test_func, arg)
future_list.append(future)
for future in future_list:
try:
print(future.result())
except Exception as e:
print(e)
这使您可以单独处理每个未来。
Ehsan的解决方案很好,但结果可能会稍微高效,而不是等待列表中的顺序项完成。以下是library docs的一个例子。
import concurrent.futures
import urllib.request
URLS = ['http://www.foxnews.com/',
'http://www.cnn.com/',
'http://europe.wsj.com/',
'http://www.bbc.co.uk/',
'http://some-made-up-domain.com/']
# Retrieve a single page and report the URL and contents
def load_url(url, timeout):
with urllib.request.urlopen(url, timeout=timeout) as conn:
return conn.read()
# We can use a with statement to ensure threads are cleaned up promptly
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
# Start the load operations and mark each future with its URL
future_to_url = {executor.submit(load_url, url, 60): url for url in URLS}
for future in concurrent.futures.as_completed(future_to_url):
url = future_to_url[future]
try:
data = future.result()
except Exception as exc:
print('%r generated an exception: %s' % (url, exc))
else:
print('%r page is %d bytes' % (url, len(data)))