简体   繁体   English

如何使用 FastAPI + uvicorn 在工作人员之间共享数据库连接?

[英]How to share database connection between workers using FastAPI + uvicorn?

I'm trying to create an app using FastAPI + uvicorn.我正在尝试使用 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.然后,我想使用 uvicorn 的 --workers --workers X选项来处理同时连接,但我需要在所有工作人员之间共享相同的数据库连接。

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.但我得到了两个 mongodb 连接。

How can I share the MongoDB connection and avoid creating a connection each single worker?如何共享 MongoDB 连接并避免为每个工作人员创建连接?

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.两个或多个进程无法成功使用单个套接字连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当 uvicorn workers>1 时 uvicorn 和 fastAPI 与 pyinstaller 问题 - uvicorn and fastAPI with pyinstaller problem when uvicorn workers>1 每个微服务有多少服务器工作人员 - FastAPI 中的 Gunicorn 和 Uvicorn - How many server workers per microservice - Gunicorn with Uvicorn in FastAPI 如何在 Google Colab 中运行 FastAPI / Uvicorn? - How to run FastAPI / Uvicorn in Google Colab? fastapi/uvicorn 如何并行化请求? - How does fastapi/uvicorn parallelize requests? 在 Uvicorn 中使用具有多个工作人员的多处理(线程锁) - Using Multiprocessing in Uvicorn with multiple workers (thread lock) 如何在 FastAPI 中的 HTTP 请求之间共享变量? - How to share variables between HTTP requests in FastAPI? 如何并行运行 uvicorn 服务器(使用 FastAPI)和负载测试(使用 locust)? - How to run a uvicorn server (using FastAPI) and a load test (using locust) parallely? Python Ray:如何在工作人员之间共享变量? - Python Ray: How to share variable between workers? 从 dockerfile 中的 uvicorn 命令和从 pythonfile 运行 fastapi 有区别吗? - is there a difference between running fastapi from uvicorn command in dockerfile and from pythonfile? 如何在 FastAPI 中进行持久数据库连接? - How to do persistent database connection in FastAPI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM