Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support starlette "lifespan" context for application #2943

@uSpike

Description

@uSpike

First check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.
  • After submitting this, I commit to:
    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
    • Or, I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
    • Implement a Pull Request for a confirmed bug.

Description

Starlette added support for the lifespan context function in 0.13.5. It's not well documented, but the code does suggest:

        # The lifespan context function is a newer style that replaces
        # on_startup / on_shutdown handlers. Use one or the other, not both.

For my purposes, it's much more convenient to use a (async)contextmanager for the startup and shutdown events of my application. It would be nice to have the option

The solution you would like

I'd like an easier way, and one that's officially supported, to use the lifespan context function.

from fastapi import FastAPI

async def lifespan(app):
    print("startup")
    async with SomeResource():
        yield
    print("shutdown")

app = FastAPI(lifespan=lifespan)

or

from fastapi import FastAPI

app = FastAPI()

@app.lifespan
async def lifespan(app):
    print("startup")
    async with SomeResource():
        yield
    print("shutdown")

Describe alternatives you've considered

I can already accomplish this simply by doing:

from fastapi import FastAPI

async def lifespan(app):
    print("startup")
    async with SomeResource():
        yield
    print("shutdown")

app = FastAPI()
app.router.lifespan_context = lifespan

however this is not officially supported and would likely break if accidentally using app.on_event in addition.

One could also do nasty stuff with __aenter__ and __aexit__:

from fastapi import FastAPI

app = FastAPI()

@app.on_event("startup")
async def startup()
    print("startup")
    app.state.resource = SomeResource()
    await app.state.resource.__aenter__()

@app.on_event("shutdown")
async def startup()
    print("shutdown")
    await app.state.resource.__aexit__(None, None, None)

but that seems quite ugly to me.

Environment

  • OS: Linux
  • FastAPI Version: 0.63.0
  • Python version: 3.8.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions