Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

551
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda