简体   繁体   English

如何确保使用 asyncio.gather 执行所有任务?

[英]How to ensure all task are executed using asyncio.gather?

When using asyncio.gather , all tasks are not executed when one of them raise exception:使用asyncio.gather时,当其中一个任务引发异常时,所有任务都不会执行:

# coding: utf-8
import asyncio


async def foo(i):
    await asyncio.sleep(0.1)
    if i == 2:
        print("2: 1/0")
        1/0
    print(i)


async def main():
    futures = []
    for i in range(1000):
        futures.append(foo(i))
    await asyncio.gather(*futures)


asyncio.run(main())
0
1
2: 1/0
3
4
5
6
7
[...]
501
502
503
504
Traceback (most recent call last):
  File "<input>", line 24, in <module>
  File "/usr/lib/python3.8/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "<input>", line 21, in main
  File "<input>", line 9, in foo
ZeroDivisionError: division by zero

How can I ensure all tasks are executed before get out of asyncio.gather ?如何确保在退出asyncio.gather之前执行所有任务? I understand this cannot be the default behavior of asyncio.gather because if one of my tasks never finished, exceptions will never be raised.我知道这不可能是asyncio.gather的默认行为,因为如果我的一项任务从未完成,则永远不会引发异常。 My question is more: How can I execute a pool of tasks gather and wait all finished/raise before continue?我的问题更多:如何执行一组任务收集并等待所有完成/提升后再继续?

I think you want to pay close attention to the documentation for asyncio.gather :我认为您想密切关注asyncio.gather 的文档

If return_exceptions is False (default), the first raised exception is immediately propagated to the task that awaits on gather().如果 return_exceptions 为 False(默认值),则第一个引发的异常会立即传播到在 gather() 上等待的任务。 Other awaitables in the aws sequence won't be cancelled and will continue to run. aws 序列中的其他等待对象不会被取消,并将继续运行。

If return_exceptions is True, exceptions are treated the same as successful results, and aggregated in the result list.如果 return_exceptions 为 True,则将异常视为成功结果,并在结果列表中聚合。

From my reading, it looks like if you were to call asyncio.gather like this...根据我的阅读,看起来如果您要像这样调用asyncio.gather ......

await asyncio.gather(*futures, return_exceptions=True)

...you should get the behavior you want. ...你应该得到你想要的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM