示例工程 20-taskgroup-cancellation-contextvars
20-taskgroup-cancellation-contextvars/02-timeout-retry/README.md
# 02 · timeout-retry
**演示**:`async with asyncio.timeout(0.02)` 在超时后取消内部等待;循环重试三次,前两次超时、第三次成功。
**运行**:
```sh
uv run python examples/20-taskgroup-cancellation-contextvars/02-timeout-retry/timeout_retry.py
```
**预期输出**(3.12.9):
```text
attempts: 3
result: ok
give up: all attempts timed out
```
**依赖**:仅标准库(Python 3.11+)。
**注意**:`asyncio.timeout` 抛的是内置 `TimeoutError`;超时会在下一个 `await` 点取消内部任务,不会被 `except Exception` 之外的清理逻辑吞掉。