FastApi is not running every request on same endpoint in separate Thread
See original GitHub issueSo, I think I understand all async def
and def
stuff. I have this piece of code and I am running it with uvicorn main:app
import time
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def root():
print("Hitted Root")
time.sleep(10)
return {"message": "Hello World"}
@app.get("/hi")
def root_hi():
print("Hitted Root Hi")
time.sleep(10)
If I visit /hi
and /
at the same time. The print statement comes instantly and they each end after 10 seconds approximately which must mean that they are starting at the same time in different threads
However if I open two requests to /hi
the first one ends and the second one then starts i.e. I see the print statement from the first one and then 10 seconds later the print statement from the second one which must mean they are not running on different threads.
I want to know why is that the case and if this is the default behaviour where requests to different endpoints run in different threads but requests to the same endpoint run one after the other. I also wonder if there is a way to make the requests to the same endpoint run in different threads and at the same time
without using multiple uvicorn workers.
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (2 by maintainers)
Top GitHub Comments
apparently no
@nilansaha How are you performing the requests? I found some time ago that (some) browsers (Firefox in my case) seem to avoid performing simultaneous requests to the same endpoint (or localhost?).
Maybe try this code to perform N concurrent requests using Python, I’d say your example would work as expected after trying it:
Output from fastapi server: