I have a web application written with python and the async framework Starlette (https://www.starlette.io/), I need to connect to MongoDB, so I installed the async pymongo driver: motor (https://motor.readthedocs.io/en/stable/) version 2.1.0
And I have some pseudo code like this:
motor_client = motor.motor_asyncio.AsyncIOMotorClient(...)
pymongo_client = pymongo.MongoClient(...)
class BlogList(HTTPEndpoint):
async def get(self, request):
blog_list = [item async for item in motor_client.blog.find(condition)]
# blog_list = list(pymongo_client.blog.find(condition))
data = {
"request": request,
"blogs": blog_list,
}
return RenderPageResponse("index.html", data)
For the blog_list line here, I benchmarked it with wrk -t10 -d10 -c100 http://localhost:8080/blog:
- If I use the
motor_clientwithawait, the result is around "1500 requests/sec" - If I change it to
pymongo_client(the sync mode), the result is around "1900 requests/sec"
Shouldn't the async mode with motor be faster than the sync mode with pymongo? I am wondering why the performance is much worse when using motor (async, 1500 req/s) compare to pymongo (sync, 1900 req/s)?
Other info: I run my starlette app with gunicorn -w 4 -k uvicorn.workers.UvicornWorker myblog:app, and just toggle these 2 lines to do the wrk benchmark.
I did the search but cannot find posts talking about this, so I am wondering did I miss something? Any help would be appreciated, thank you.