python
runtime_error
ai_generated
true
运行时错误:异步生成器引发了 StopAsyncIteration
RuntimeError: async generator raised StopAsyncIteration
ID: python/starlette-async-iterator-exhausted
80%修复率
81%置信度
0证据数
2024-09-14首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.10 | active | — | — | — |
| 3.11 | active | — | — | — |
根因分析
Starlette 的异步生成器在迭代过程中被提前关闭或耗尽
English
Starlette 的异步生成器在迭代过程中被提前关闭或耗尽
解决方案
-
90% 成功率 确保异步生成器正确使用 async for 循环
async for item in async_generator(): process(item) -
85% 成功率 在调用处捕获 StopAsyncIteration
try: async for item in gen: ... except StopAsyncIteration: pass
无效尝试
常见但无效的做法:
-
在生成器函数中添加 try-except 捕获 StopAsyncIteration
80% 失败
生成器内部不应捕获此异常,会导致状态混乱
-
使用同步生成器替代
70% 失败
破坏了异步上下文,可能阻塞事件循环