【发布时间】:2020-07-02 18:28:11
【问题描述】:
我正在使用 python 的concurrent.futures 中的ThreadPoolExecutor 来并行化抓取并将结果写入数据库。这样做时,我意识到如果其中一个线程失败,我不会得到任何信息。 我怎样才能正确地知道哪些线程失败以及为什么失败(所以使用“正常”回溯)?下面是一个最小的工作示例。
import logging
logging.basicConfig(format='%(asctime)s %(message)s',
datefmt='%y-%m-%d %H:%M:%S', level=logging.INFO)
from concurrent.futures import ThreadPoolExecutor
def worker_bee(seed):
# sido is not defined intentionally to break the code
result = seed + sido
return result
# uncomment next line, and you will get the usual traceback
# worker_bee(1)
# ThreadPoolExecutor will not provide any traceback
logging.info('submitting all jobs to the queue')
with ThreadPoolExecutor(max_workers=4) as executor:
for seed in range(0,10):
executor.submit(worker_bee, seed)
logging.info(f'submitted, waiting for threads to finish')
如果我在worker_bee() 中导入日志并将消息定向到根记录器,我可以在最终日志中看到这些消息。但是我只能看到我定义的日志消息,而不是代码实际失败的追溯。
【问题讨论】:
标签: python python-multithreading