python
runtime_error
ai_generated
true
运行时错误: ASGI应用程序实例不可用。
RuntimeError: The ASGI application instance is not available.
ID: python/starlette-missing-app-instance
80%修复率
85%置信度
0证据数
2024-02-28首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
尝试在Starlette应用之外访问app实例,例如在导入时或测试中未正确设置。
English
尝试在Starlette应用之外访问app实例,例如在导入时或测试中未正确设置。
解决方案
-
95% 成功率 在应用上下文中访问app,例如通过request.app
from starlette.requests import Request; async def my_endpoint(request: Request): app = request.app; return JSONResponse({'app': str(app)}) -
90% 成功率 在测试中使用TestClient时确保app已创建
from starlette.testclient import TestClient; from main import app; client = TestClient(app); response = client.get('/')
无效尝试
常见但无效的做法:
-
在模块级别直接访问app对象
80% 失败
app可能尚未创建或已被回收。
-
使用全局变量存储app引用
70% 失败
多线程环境下可能导致竞态条件。