[Python] How to add a shutdown event in FastAPI using lifespan in middleware?

Discussão em 'Python' iniciado por Stack, Abril 12, 2025.

  1. Stack

    Stack Membro Participativo

    FastAPI profiler code

    if server_app is not None:
    server_app.add_event_handler("shutdown", self.get_profiler_result)


    I think Starlette is not allowing both lifespan and shutdown events. Code link

    if on_startup or on_shutdown:
    warnings.warn(
    "The on_startup and on_shutdown parameters are deprecated, and they "
    "will be removed on version 1.0. Use the lifespan parameter instead. "
    "See more about it on https://www.starlette.io/lifespan/.",
    DeprecationWarning,
    )
    if lifespan:
    warnings.warn(
    "The `lifespan` parameter cannot be used with `on_startup` or "
    "`on_shutdown`. Both `on_startup` and `on_shutdown` will be "
    "ignored."
    )


    I am using lifespan to manage my DB and that cannot be removed. Can you suggest a way ?

    I tried to check in the __call__ function whether the scope is "lifespan" and send event is lifespan.shutdown acc. to link but did not get that event.

    My code:

    app = FastAPI(
    lifespan=lifespan,
    )

    app.add_middleware(PyInstrumentProfilerMiddleware,
    profiler_output_type="html",
    server_app=app,
    open_in_browser=True)

    Continue reading...

Compartilhe esta Página