Detect failed tasks in concurrent.futures

前端 未结 2 2055
野趣味
野趣味 2021-01-17 17:43

I\'ve been using concurrent.futures as it has a simple interface and let user easily control the max number of threads/processes. However, it seems like concurrent.futures h

相关标签:
2条回答
  • 2021-01-17 18:18

    concurrent.futures.wait will ensure all the tasks completed, but it doesn't check success (something return-ed) vs. failure (exception raised and not caught in worker function). To do that, you need to call .result() on each Future (which will cause it to either re-raise the exception from the task, or produce the return-ed value). There are other methods to check without actually raising in the main thread (e.g. .exception()), but .result() is the most straightforward method.

    If you want to make it re-raise, the simplest approach is just to replace the wait() call with:

    for fut in concurrent.futures.as_completed(fs):
        fut.result()
    

    which will process results as Futures complete, and promptly raise an Exception if one occurred. Alternatively, you continue to use wait so all tasks finish before you check for exceptions on any of them, then iterate over fs directly and call .result() on each.

    0 讨论(0)
  • 2021-01-17 18:30

    There is another way to do the same with multiprocessing.Pool (for processes) or multiprocessing.pool.ThreadPool (for threads). As far as I know it rethrows any caught exceptions.

    0 讨论(0)
提交回复
热议问题