聚集与异步。等待

asyncio.gather asyncio.wait似乎有类似的用途: 我有一堆异步事件需要执行/等待(不一定要等到一个异步事件完成后才能开始下一个异步事件)。它们使用不同的语法,并且在一些细节上有所不同,但是对我来说,有两个功能有如此巨大的重叠的函数似乎非常不合常理。我错过了什么?

258489 次浏览

虽然在一般情况下类似(“运行并获得许多任务的结果”),但每个函数对于其他情况都有一些特定的功能:

asyncio.gather()

返回一个Future实例,允许对任务进行高级分组:

import asyncio
from pprint import pprint


import random




async def coro(tag):
print(">", tag)
await asyncio.sleep(random.uniform(1, 3))
print("<", tag)
return tag




loop = asyncio.get_event_loop()


group1 = asyncio.gather(*[coro("group 1.{}".format(i)) for i in range(1, 6)])
group2 = asyncio.gather(*[coro("group 2.{}".format(i)) for i in range(1, 4)])
group3 = asyncio.gather(*[coro("group 3.{}".format(i)) for i in range(1, 10)])


all_groups = asyncio.gather(group1, group2, group3)


results = loop.run_until_complete(all_groups)


loop.close()


pprint(results)

组中的所有任务都可以通过调用group2.cancel()甚至all_groups.cancel()来取消。参见.gather(..., return_exceptions=True)

asyncio.wait()

支持在第一个任务完成后等待停止,或在指定的超时后,允许较低的操作精度:

import asyncio
import random




async def coro(tag):
print(">", tag)
await asyncio.sleep(random.uniform(0.5, 5))
print("<", tag)
return tag




loop = asyncio.get_event_loop()


tasks = [coro(i) for i in range(1, 11)]


print("Get first result:")
finished, unfinished = loop.run_until_complete(
asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED))


for task in finished:
print(task.result())
print("unfinished:", len(unfinished))


print("Get more results in 2 seconds:")
finished2, unfinished2 = loop.run_until_complete(
asyncio.wait(unfinished, timeout=2))


for task in finished2:
print(task.result())
print("unfinished2:", len(unfinished2))


print("Get all other results:")
finished3, unfinished3 = loop.run_until_complete(asyncio.wait(unfinished2))


for task in finished3:
print(task.result())


loop.close()

asyncio.waitasyncio.gather更低级。

顾名思义,asyncio.gather主要集中于收集结果。它等待一堆期货,并以给定的顺序返回结果。

asyncio.wait只是等待期货。它不会直接给出结果,而是给出已完成和待完成的任务。您必须手动收集这些值。

此外,你可以指定等待所有期货完成,或者只等待第一个期货wait

我还注意到你可以在wait()中通过简单地指定列表来提供一组协程:

result=loop.run_until_complete(asyncio.wait([
say('first hello', 2),
say('second hello', 1),
say('third hello', 4)
]))

而在gather()中分组是通过指定多个协程来完成的:

result=loop.run_until_complete(asyncio.gather(
say('first hello', 2),
say('second hello', 1),
say('third hello', 4)
))

很容易忽略的一个非常重要的区别是,当涉及到异常时,这两个函数的默认行为。


我将使用这个示例来模拟一个会引发异常的协程,有时是-

import asyncio
import random




async def a_flaky_tsk(i):
await asyncio.sleep(i)  # bit of fuzz to simulate a real-world example


if i % 2 == 0:
print(i, "ok")
else:
print(i, "crashed!")
raise ValueError


coros = [a_flaky_tsk(i) for i in range(10)]

await asyncio.gather(*coros)输出-

0 ok
1 crashed!
Traceback (most recent call last):
File "/Users/dev/PycharmProjects/trading/xxx.py", line 20, in <module>
asyncio.run(main())
File "/Users/dev/.pyenv/versions/3.8.2/lib/python3.8/asyncio/runners.py", line 43, in run
return loop.run_until_complete(main)
File "/Users/dev/.pyenv/versions/3.8.2/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
return future.result()
File "/Users/dev/PycharmProjects/trading/xxx.py", line 17, in main
await asyncio.gather(*coros)
File "/Users/dev/PycharmProjects/trading/xxx.py", line 12, in a_flaky_tsk
raise ValueError
ValueError

如你所见,索引1后的coros永远不会执行。


但是await asyncio.wait(coros)继续执行任务,即使其中一些任务失败

0 ok
1 crashed!
2 ok
3 crashed!
4 ok
5 crashed!
6 ok
7 crashed!
8 ok
9 crashed!
Task exception was never retrieved
future: <Task finished name='Task-10' coro=<a_flaky_tsk() done, defined at /Users/dev/PycharmProjects/trading/xxx.py:6> exception=ValueError()>
Traceback (most recent call last):
File "/Users/dev/PycharmProjects/trading/xxx.py", line 12, in a_flaky_tsk
raise ValueError
ValueError
Task exception was never retrieved
future: <Task finished name='Task-8' coro=<a_flaky_tsk() done, defined at /Users/dev/PycharmProjects/trading/xxx.py:6> exception=ValueError()>
Traceback (most recent call last):
File "/Users/dev/PycharmProjects/trading/xxx.py", line 12, in a_flaky_tsk
raise ValueError
ValueError
Task exception was never retrieved
future: <Task finished name='Task-2' coro=<a_flaky_tsk() done, defined at /Users/dev/PycharmProjects/trading/xxx.py:6> exception=ValueError()>
Traceback (most recent call last):
File "/Users/dev/PycharmProjects/trading/xxx.py", line 12, in a_flaky_tsk
raise ValueError
ValueError
Task exception was never retrieved
future: <Task finished name='Task-9' coro=<a_flaky_tsk() done, defined at /Users/dev/PycharmProjects/trading/xxx.py:6> exception=ValueError()>
Traceback (most recent call last):
File "/Users/dev/PycharmProjects/trading/xxx.py", line 12, in a_flaky_tsk
raise ValueError
ValueError
Task exception was never retrieved
future: <Task finished name='Task-3' coro=<a_flaky_tsk() done, defined at /Users/dev/PycharmProjects/trading/xxx.py:6> exception=ValueError()>
Traceback (most recent call last):
File "/Users/dev/PycharmProjects/trading/xxx.py", line 12, in a_flaky_tsk
raise ValueError
ValueError

当然,可以通过使用-来改变这两种行为

asyncio.gather(..., return_exceptions=True)

或者,

asyncio.wait([...], return_when=asyncio.FIRST_EXCEPTION)


但这还没完!

< p >注意: Task exception was never retrieved

. log

. log

asyncio.wait()不会从子任务重新引发异常,直到你分别await它们。(日志中的堆栈跟踪只是消息,它们不能被捕获!)

done, pending = await asyncio.wait(coros)
for tsk in done:
try:
await tsk
except Exception as e:
print("I caught:", repr(e))

输出-

0 ok
1 crashed!
2 ok
3 crashed!
4 ok
5 crashed!
6 ok
7 crashed!
8 ok
9 crashed!
I caught: ValueError()
I caught: ValueError()
I caught: ValueError()
I caught: ValueError()
I caught: ValueError()

另一方面,要用asyncio.gather()捕获异常,必须-

results = await asyncio.gather(*coros, return_exceptions=True)
for result_or_exc in results:
if isinstance(result_or_exc, Exception):
print("I caught:", repr(result_or_exc))

(输出与之前相同)

除了前面所有的答案,我想谈谈gather()wait()的不同行为,以防它们是取消了

收集()取消

如果gather()被取消,则所有提交的等待对象(尚未完成的)也将被取消.;

Wait ()取消

如果wait()ing任务被取消,它只抛出CancelledError,等待的任务保持不变。

简单的例子:

import asyncio




async def task(arg):
await asyncio.sleep(5)
return arg




async def cancel_waiting_task(work_task, waiting_task):
await asyncio.sleep(2)
waiting_task.cancel()
try:
await waiting_task
print("Waiting done")
except asyncio.CancelledError:
print("Waiting task cancelled")


try:
res = await work_task
print(f"Work result: {res}")
except asyncio.CancelledError:
print("Work task cancelled")




async def main():
work_task = asyncio.create_task(task("done"))
waiting = asyncio.create_task(asyncio.wait({work_task}))
await cancel_waiting_task(work_task, waiting)


work_task = asyncio.create_task(task("done"))
waiting = asyncio.gather(work_task)
await cancel_waiting_task(work_task, waiting)




asyncio.run(main())

输出:

asyncio.wait()
Waiting task cancelled
Work result: done
----------------
asyncio.gather()
Waiting task cancelled
Work task cancelled

应用

有时需要结合wait()gather()功能。例如,我们希望等待至少一个任务的完成,然后等待取消其余的挂起任务,如果waiting本身是取消了,那么同时取消所有待处理的任务。

作为真实的例子,假设我们有一个断开连接事件和一个工作任务。我们希望等待工作任务的结果,但如果连接丢失,则取消它。或者,我们将发出几个并行请求,但在完成至少一个响应后,取消所有其他请求。

可以这样做:

import asyncio
from typing import Optional, Tuple, Set




async def wait_any(
tasks: Set[asyncio.Future], *, timeout: Optional[int] = None,
) -> Tuple[Set[asyncio.Future], Set[asyncio.Future]]:
tasks_to_cancel: Set[asyncio.Future] = set()
try:
done, tasks_to_cancel = await asyncio.wait(
tasks, timeout=timeout, return_when=asyncio.FIRST_COMPLETED
)
return done, tasks_to_cancel
except asyncio.CancelledError:
tasks_to_cancel = tasks
raise
finally:
for task in tasks_to_cancel:
task.cancel()




async def task():
await asyncio.sleep(5)




async def cancel_waiting_task(work_task, waiting_task):
await asyncio.sleep(2)
waiting_task.cancel()
try:
await waiting_task
print("Waiting done")
except asyncio.CancelledError:
print("Waiting task cancelled")


try:
res = await work_task
print(f"Work result: {res}")
except asyncio.CancelledError:
print("Work task cancelled")




async def check_tasks(waiting_task, working_task, waiting_conn_lost_task):
try:
await waiting_task
print("waiting is done")
except asyncio.CancelledError:
print("waiting is cancelled")


try:
await waiting_conn_lost_task
print("connection is lost")
except asyncio.CancelledError:
print("waiting connection lost is cancelled")


try:
await working_task
print("work is done")
except asyncio.CancelledError:
print("work is cancelled")




async def work_done_case():
working_task = asyncio.create_task(task())
connection_lost_event = asyncio.Event()
waiting_conn_lost_task = asyncio.create_task(connection_lost_event.wait())
waiting_task = asyncio.create_task(wait_any({working_task, waiting_conn_lost_task}))
await check_tasks(waiting_task, working_task, waiting_conn_lost_task)




async def conn_lost_case():
working_task = asyncio.create_task(task())
connection_lost_event = asyncio.Event()
waiting_conn_lost_task = asyncio.create_task(connection_lost_event.wait())
waiting_task = asyncio.create_task(wait_any({working_task, waiting_conn_lost_task}))
await asyncio.sleep(2)
connection_lost_event.set()  # <---
await check_tasks(waiting_task, working_task, waiting_conn_lost_task)




async def cancel_waiting_case():
working_task = asyncio.create_task(task())
connection_lost_event = asyncio.Event()
waiting_conn_lost_task = asyncio.create_task(connection_lost_event.wait())
waiting_task = asyncio.create_task(wait_any({working_task, waiting_conn_lost_task}))
await asyncio.sleep(2)
waiting_task.cancel()  # <---
await check_tasks(waiting_task, working_task, waiting_conn_lost_task)




async def main():
print("Work done")
print("-------------------")
await work_done_case()
print("\nConnection lost")
print("-------------------")
await conn_lost_case()
print("\nCancel waiting")
print("-------------------")
await cancel_waiting_case()




asyncio.run(main())

输出:

Work done
-------------------
waiting is done
waiting connection lost is cancelled
work is done


Connection lost
-------------------
waiting is done
connection is lost
work is cancelled


Cancel waiting
-------------------
waiting is cancelled
waiting connection lost is cancelled
work is cancelled