python
config_error
ai_generated
true
RuntimeError: Lifespan protocol not supported. Use 'lifespan' parameter.
ID: python/starlette-missing-lifespan
80%Fix Rate
85%Confidence
0Evidence
2024-05-10First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
Starlette applications require the lifespan parameter to be set when using async context managers for startup/shutdown.
generic中文
Starlette 应用程序在使用异步上下文管理器处理启动/关闭时需要设置 lifespan 参数。
Workarounds
-
95% success Define a lifespan context manager
from contextlib import asynccontextmanager @asynccontextmanager async def lifespan(app): # startup yield # shutdown app = Starlette(lifespan=lifespan) -
90% success Use on_startup and on_shutdown callbacks
app = Starlette(on_startup=[startup_func], on_shutdown=[shutdown_func])
Dead Ends
Common approaches that don't work:
-
Not using any lifespan parameter
100% fail
The application expects explicit lifespan handling.
-
Setting lifespan to a string like 'auto'
90% fail
The lifespan parameter must be a callable or None.