python - FastAPI - pymongo + sync routes seemingly MUCH faster than motor + async routes - Stack Overflow

I am using FastAPI + pymongo (sync routes) and saw motor was recommended for 'best practices'

I am using FastAPI + pymongo (sync routes) and saw motor was recommended for 'best practices' on FastAPI.

I set up two routes with the same DB call, one using async router + motor, one using sync + pymongo.

The async route is insanely slow, taking 27 seconds on average to respond with 300 users (~60 requests per second), so i assume I must be doing something wrong here. Please let me know where I am going wrong! The pymongo sync router is super fast with 50ms responses for same test.

# db.py

client = AsyncIOMotorClient(
    mongo_con_string,
)
sync_client = MongoClient(
    mongo_con_string,
)

mongo_db = client["test"]
sync_mongo_db = sync_client["test"]

#views.py

@test_router.get("/test", status_code=200, response_description="")
async def get_articles():
        collection = mongo_db[TEST_TABLE]
        articles = await collection.find().to_list(1000)
        articles_str = json.dumps(articles, default=str)


@test_router.get("/test-sync", status_code=200, response_description="")
def get_articles_sync():
        collection = sync_mongo_db[TEST_TABLE]
        articles = collection.find().to_list(1000)
        articles_str = json.dumps(articles, default=str)

I added workers in the dockerfile, added more CPU/Memory, extra tasks on ECS but nothing seems to be working. Is this not an issue of FastAPI and potentially my mongo instance itself? When I tested 300 users on a simple /health endpoint the results were very fast.

# Dockerfile

FROM python:3.11

WORKDIR /code

COPY ./api/requirements.txt /code/requirements.txt

RUN pip install --upgrade -r /code/requirements.txt

COPY ./api/app /code/app
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80", "--proxy-headers", "--workers", "10"]


I read this answer along with the documentation - my understanding is that as we await a DB call it will allow other requests to come in and start to be processed as it finishes. But from my tests it seems like its just blocking the event loop completely with each call.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742292261a4416419.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信