I'm new to Python and I'm working on a FastAPI & peewee application. I want to manage the database connection pool explicitly so according to the framework integration documentation for FastAPI I use the startup/shutdown events.
database = PooledSqliteDatabase('foobar.db', autoconnect=False, stale_timeout=60)
with database:
database.create_tables([models.Foobar])
app = FastAPI()
app.include_router(foobar_router)
@app.on_event("startup")
def startup():
database.connect()
@app.on_event("shutdown")
def shutdown():
if not database.is_closed():
database.close()
The problem is that these events do not correspond to "open the connection when a request is received, then close it when the response is returned", but when the whole app starts and stops. More importantly I don't understand why I get "Error, database connection not opened." when I access the database in my router as it should still be opened at that point? It only seems to work when I add async
to my routes.
I guess I could switch to some proper "before/after request" middleware, but at this point I'm wondering if my approach is just completely wrong.
Edit: I tried using middleware, but while this unsurprisingly works as intended, I still have the issue that I get "Error, database connection not opened." unless I add async
to my routes.
@app.middleware("http")
async def with_database(request: Request, call_next):
with database:
response = await call_next(request)
return response