python config_error ai_generated true

RuntimeError: Lifespan protocol not supported. Use 'lifespan' parameter.

ID: python/starlette-missing-lifespan

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-05-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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

  1. 95% success Define a lifespan context manager
    from contextlib import asynccontextmanager
    
    @asynccontextmanager
    async def lifespan(app):
        # startup
        yield
        # shutdown
    
    app = Starlette(lifespan=lifespan)
  2. 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:

  1. Not using any lifespan parameter 100% fail

    The application expects explicit lifespan handling.

  2. Setting lifespan to a string like 'auto' 90% fail

    The lifespan parameter must be a callable or None.