示例工程 18-asyncio-event-loop

18-asyncio-event-loop/01-serial-vs-gather/README.md

markdown · 0.6 KB

# 01 · serial-vs-gather
 
**演示**:三个 `asyncio.sleep(0.05)` 串行 `await` 约 0.15s;`asyncio.gather` 并发等待约 0.05s,总耗时接近单个任务。
**运行**:
 
```sh
uv run python examples/18-asyncio-event-loop/01-serial-vs-gather/serial_vs_gather.py
```
 
**预期输出**(3.12.9,耗时随机器波动):
 
```text
serial: ['a', 'b', 'c'] 0.15s
gather: ['a', 'b', 'c'] 0.05s
speedup: 3.0x
```
 
**依赖**:仅标准库。
**注意**:并发的前提是任务在 `await` 处让出;如果任务里是 CPU 计算或 `time.sleep`,`gather` 不会加速(见 02)。