Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

547
Views
How to share database connection between workers using FastAPI + uvicorn?

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 ?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!