How to store global 'driver' variable (FastAPI Python)

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

from db import get_driver()

drivers = {}

@asynccontextmanager
async def lifespan(app: FastAPI):
    drivers["db"] = await get_driver()
    yield
    await drivers["db"].close()

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 :confused:

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?

With serverless functions, you might have multiple instances and cannot save file changes to the server after deployment. That can cause problems like what you’re seeing. It can take a bit of adjustment to get a traditional server type of project working in a serverless environment.

There’s a Next.js FastAPI Starter that might help, though it doesn’t use exactly the same setup that you shared.

No DB connection is made or maintained on the FastAPI side of things in this template :confused:
So this is not really an answer to the question.

I saw in the other post you mentioned a project named Artha.ai. They split resources up between Vercel (for the frontend) and Render (for the FastAPI back-end). I’ll have to check to see if this is related to the serverless limitations

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.