I have implemented all my routes using async. And have followed all guidelines from the FastAPI documentation.
Every route has multiple DB calls, which does not have async support, so they are normal function like this
def db_fetch(query):
# I take a few seconds to respond
return
To avoid blocking my event loop I use fastapi.concurrancy.run_in_threadpool
Now the issue is, when a large number of requests come, my new requests are getting blocked. Even if I close the browser tab (cancel request), the entire app gets stuck till the older requests get processed.
What am I doing wrong here?
I use uvicorn
as my ASGI server. I run in a kubernetes cluster with 2 replica.
Few suspects: Am I spawning too many threads? Is it some bug within uvicron? Not really sure!
run_in_threadpool
as it uses AnyIO to manage threads, and takes care of housekeeping. – Housewarming