So, 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.