Featured image of post Python asyncio协程 - 简单等待(二)

Python asyncio协程 - 简单等待(二)

asyncio协程 - 简单等待(二)

语法如下

1
2

asyncio.as_completed(aws, *, loop=None, timeout=None)

官方文档解释

并发地运行 aws 集合中的 可等待对象。 返回一个协程的迭代器。 所返回的每个协程可被等待以从剩余的可等待对象集合中获得最早的下一个结果。

如果在所有 Future 对象完成前发生超时则将引发 asyncio.TimeoutError

比如下面的示例

1
2
3
4

for coro in as_completed(aws):
    earliest_result = await coro
    # ...

我们看一个完整的示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18

import asyncio
import time

async def delay_timer(delay):
    await asyncio.sleep(delay)
    print(f"delay for {delay} seconds")
    return delay

async def main():
    print(f"start at {time.strftime('%X')}")
    tasks = [delay_timer(i) for i in range(10)]
    for p in asyncio.as_completed(tasks):
        res = await p

    print(f"end at {time.strftime('%X')}")

asyncio.run(main())

运行结果如下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14

$ python main.py
start at 22:23:48
delay for 0 seconds
delay for 1 seconds
delay for 2 seconds
delay for 3 seconds
delay for 4 seconds
delay for 5 seconds
delay for 6 seconds
delay for 7 seconds
delay for 8 seconds
delay for 9 seconds
end at 22:23:57
Licensed under CC BY-NC-SA 4.0
最后更新于 Jul 30, 2025 11:04 +0800