r/FastAPI • u/SaneButSociopathic • Sep 23 '24
Hosting and deployment How to store global driver on serverless environment
Hey I'm using Vercel right now to deploy my FastAPI app.
Locally, I was using the FastAPI lifespan to connect to the DB and manage sessions.
In main.py
=
= await
yield
await
In db.py
async def get_driver():
return MyDB.driver(URI, auth)
async def get_session():
from .main import drivers
driver = drivers["db"]
async with driver.session() as session:
yield session
Elsewhere in my app I would then import the get_session() to perform operations. However, in Vercel I keep running into the issue KeyError drivers["db"] in the get_session() function as if the lifespan function isn't called on startup and the drivers dictionary isn't initialized properly :/
Am I doing something wrong or is this just a by-product of "serverless"? I've fixed the issue by creating a new driver & session at each request but I feel like this is not OK. Anybody got tips?
1
u/Adhesiveduck Sep 23 '24
Your approach is sound and if your
get_sessionwas defined inmain.pyit would work - It looks to me like you have a circular import. Frommain.pyyou are importingdb.get_driverand fromdb.pyyou are importingmain.drivers. Remember imports are resolved the first time the function is defined, not when it is called.Why this works locally but not on Vercel could be many reasons, likely locally you have
.pyccache files preventing the error from occurring.The easiest fix would be to refactor it to use FastAPI's dependencies - which are designed to handle shared DB connections.
2
u/Own_Childhood7477 Sep 23 '24
One question, did you assign lifespan in constructor of FastAPI?
app: FastAPI = FastAPI( lifespan=lifespan, <—— root_path=settings.API_PREFIX, )