python
runtime_error
ai_generated
true
asyncio异常:睡眠被中断
asyncio.exceptions.CancelledError: sleep interrupted
ID: python/asyncio-sleep-interrupt
80%修复率
87%置信度
0证据数
2025-05-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.7 | active | — | — | — |
| 3.8 | active | — | — | — |
根因分析
在asyncio.sleep()期间任务被取消,这是一个常见的取消点。
English
A task is cancelled during asyncio.sleep(), which is a common cancellation point.
解决方案
-
95% 成功率
try: await asyncio.sleep(10) except asyncio.CancelledError: raise
-
80% 成功率
await asyncio.shield(asyncio.sleep(10))
无效尝试
常见但无效的做法:
-
90% 失败
Blocks the event loop and defeats the purpose of asyncio.
-
70% 失败
May lead to resource leaks and inconsistent state.