-
First Check
Commit to Help
Example Codefrom contextlib import asynccontextmanager
from faker import Faker
from fastapi import FastAPI
@asynccontextmanager
async def app_lifespan(app: FastAPI):
for _ in range(100):
await User.create(
name=faker.name(),
email=faker.email(),
)
app = FastAPI(lifespan=app_lifespan)
register_tortoise(
app,
db_url="sqlite://data.sqlite3",
modules={"models": [__name__]},
generate_schemas=True,
)
if __name__ == "__main__":
uvicorn.run("testapp:app", port=7000, reload=True) DescriptionI'm encountering an error with FastAPI's lifespan coroutine I'm trying to use the lifespan functionality in a FastAPI application to perform some startup tasks, like creating users in this case. However, I'm running into the error Expected BehaviorI expect uvicorn to automatically await the app_lifespan coroutine, which would trigger the defined startup logic before the application starts handling requests. This would ensure tasks like user creation happen before the app is live. Current BehaviorThe error message indicates that uvicorn is not awaiting the app_lifespan coroutine. Operating SystemLinux Operating System DetailsUbuntu 23.10 FastAPI Version0.110.0 Pydantic Version2.6.4 Python Version3.10.9 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Missing yield on lifespan. |
Beta Was this translation helpful? Give feedback.
Missing yield on lifespan.