Controlling and understanding parallel requests #87
-
Due to some issues with requests that take a long time, we wondered about how many requests are actually run in parallel / concurrently. We don't necessarily want to have a large limit, but we want to be able to control the maximal number of requests processed in parallel. FastAPI uses Starlette, which does parallelization with a thread-pool using AnyIO. So why does it make sense to still have gunicorn with multiple workers on top? (In this case we define our controller method without asyncio, so no concurrency is used.) The default AnyIO threadpool has size 40, so that is not the limiting factor. Uvicorn also has some options for Could anybody enlighten me? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I did some testing and found the following. Most likely the observation of a maximum of 7 parallel requests handled was not correct and all requests received by the service where handled in parallel. There is a vital difference between using When using When using I didn't find a built-in way in FastAPI to limit the concurrency per worker, but a standard Sources:
|
Beta Was this translation helpful? Give feedback.
-
Thank you for sharing some insights on this. Concurrency can be difficult to understand and control.
Yes, definitely. I think it's summarized well in the post you shared:
This means that
There is some interest in customizing the size of the Starlette threadpool (encode/starlette#1724).
Each Gunicorn worker runs a separate FastAPI app instance. If using a Uvicorn worker with Gunicorn, concurrency limits can be specified through Uvicorn. Uvicorn has a The solution recommended in the Uvicorn docs is to subclass from uvicorn.workers import UvicornWorker
class MyUvicornWorker(UvicornWorker):
CONFIG_KWARGS = {"loop": "auto", "http": "auto", "limit_concurrency": 30} Let's say this subclass is in |
Beta Was this translation helpful? Give feedback.
Thank you for sharing some insights on this. Concurrency can be difficult to understand and control.
Yes, definitely. I think it's summarized well in the post you shared: