MongoEngine / mongoengine

A Python Object-Document-Mapper for working with MongoDB
http://mongoengine.org
MIT License
4.24k stars 1.23k forks source link

handle multiple api requests #2853

Closed tvone closed 1 month ago

tvone commented 1 month ago

If mongoengine doesn't support async/await, how can python handle multiple api requests at the same time? Can someone help me? Currently my api function is synchronous, it cannot handle multiple get requests at the same time

jeonghanjoo commented 1 month ago

If you use a async-support server framework like fastAPI, you can use something like run_in_threadpool(supported by starlette in fastAPI).

If you want to be in pure python, you can use run_in_executor from asyncio.

I am not saying these ways are more faster or performance-better but you can handle the multiple requests at the same time anyway.

If you don't have to do with server-side works, just running in multiple processes can be easier or faster way.

bagerard commented 1 month ago

Correct so MongoEngine relies on Pymongo under the hood which is sync-only. MongoEngine and Pymongo are thread-safe so they can work in a multi-threaded application, typically a web application (flask, fastapi, etc). So basically you need to check how to adapt the framework (e.g FastAPi) to run multiple threads / run multiple worker/instances. Closing as this is not a MongoEngine issue