我正在使用ThreadPoolExecutorpythonconcurrent.futures并行抓取结果并将结果写入数据库。这样做时,我意识到如果其中一个线程失败,我将无法获得任何信息。我怎样才能正确地知道哪些线程失败以及为什么失败(因此使用“正常”回溯)?下面是一个最小的工作示例。
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')
Run Code Online (Sandbox Code Playgroud)
如果我在内部导入日志记录 …