6
votes

I'm trying to create an app using FastAPI + uvicorn.

This app must be able to handle simultaneous connections. I cannot guarantee that all the code can be executed in a async/await way.

Then, I thought to use the --workers X options from uvicorn to handle simultaneous connections, but I need to share the same database connection among all the workers.

I tried the following example:

import time
from pymongo.database import Database
from fastapi import Depends, FastAPI
from dynaconf import settings
from pymongo import MongoClient

print("-------> Creating a new MongoDB connection")
db_conn = MongoClient(settings.MONGODB_URI)
db = db_conn.get_database('mydb')

app = FastAPI()

def get_db():
    return db

@app.get("/{id}")
async def main(id: str, db: Database = Depends(get_db)):
    print("Recebido id: " + id)
    time.sleep(10)
    return { 'id': id }
$uvicorn main:app --workers 2
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started parent process [24730]
-------> Creating a new MongoDB connection
-------> Creating a new MongoDB connection
INFO:     Started server process [24733]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Started server process [24732]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

But I'm getting two mongodb connections.

How can I share the MongoDB connection and avoid creating a connection each single worker ?

1
Hi @kleyson-rios, did you ever find a solution to this? - Mabyn
@Mabyn No, I didn't. - Kleyson Rios

1 Answers

2
votes

You must not share the connection, as it's stateful. Two or more processes couldn't be able to use a single socket connection successfully.