r/FastAPI • u/musava_ribica • Nov 24 '24
Question actual difference between synchronous and asynchronous endpoints
Let's say I got these two endpoints
return
await
return
The server is run as usual using uvicorn main:app --host 0.0.0.0 --port 7777
When I open /test1 in my browser it takes ten seconds to load which makes sense.
When I open two tabs of /test1, it takes 10 seconds to load the first tab and another 10 seconds for the second tab.
However, the same happens with /test2 too. That's what I don't understand, what's the point of asynchronous being here then? I expected if I open the second tab immediately after the first tab that 1st tab will load after 10s and the 2nd tab just right after.
I know uvicorn has a --workers option but there is this background task in my app which repeats and there must be only one running at once, if I increase the workers, each will spawn another instance of that running task which is not good
View all comments
Show parent comment
1
u/conogarcia Nov 24 '24
also, FastAPI uses anyio which comes with a default thread pool size of 40. meaning, more than 40 sync requests and the threadpool has to wait for a thread to be free.
You can increase this limit though.