# RuntimeWarning: 协程 'sleep' 从未被 await —— 你是想用 time.sleep 吗？

- **ID:** `python/asyncio-sleep-blocks-loop`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在异步函数中调用了 time.sleep()，阻塞了整个事件循环，而不是让出控制权。

## 版本兼容性

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

## 解决方案

1. **** (98% 成功率)
   ```
   Use asyncio.sleep:

import asyncio
await asyncio.sleep(1)
   ```
2. **** (90% 成功率)
   ```
   For CPU-bound work, offload to a thread:

await asyncio.to_thread(cpu_heavy_fn)
   ```
3. **** (85% 成功率)
   ```
   Enable asyncio debug mode to catch blocking calls:

asyncio.run(main(), debug=True)
   ```

## 无效尝试

- **** — Still blocks the loop, just for less time. (90% 失败率)
- **** — Works but wastes a thread for what asyncio.sleep does natively. (50% 失败率)
- **** — Overcomplicated; the blocking call still stalls that thread's loop. (70% 失败率)
