# asyncio.exceptions.TimeoutError: 连接主机 https://slow.example.com 超时

- **ID:** `python/aiohttp-timeout-total`
- **领域:** python
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

aiohttp ClientTimeout（total 或 sock_connect）在服务器响应之前耗尽；默认 total 为 5 分钟，但 sock_connect 只有 30 秒。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |
| 3.10 | active | — | — |
| 3.11 | active | — | — |
| 3.12 | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   Configure explicit timeouts:

timeout = aiohttp.ClientTimeout(total=30, connect=5, sock_read=10)
async with aiohttp.ClientSession(timeout=timeout) as s:
    await s.get(url)
   ```
2. **** (90% 成功率)
   ```
   Wrap with asyncio.wait_for for a per-call ceiling:

await asyncio.wait_for(session.get(url), timeout=15)
   ```
3. **** (88% 成功率)
   ```
   Use aiohttp-retry for exponential backoff on TimeoutError.
   ```

## 无效尝试

- **** — Requests hang forever on dead servers, exhausting the connection pool. (85% 失败率)
- **** — Masks slow endpoints but ties up sockets and worker slots. (60% 失败率)
- **** — Hammers an already-slow server and can trigger rate limits. (70% 失败率)
