Search code examples
pythonasynchronousfastapiuvicorn

FastAPI - why can server handle several requests at the same time in synchronous mode


I run the following program:

import time
from datetime import datetime
from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def root():
    print(f"Started at {datetime.now()}")
    time.sleep(30)
    print(f"Executed at {datetime.now()}")
    return {"message": "Hello World"}

with uvicorn. Then I open in browser http://127.0.0.1:8000/ in two different tabs in a short period of time. The output is like this:

Started at 2023-04-01 23:40:53.668811
Started at 2023-04-01 23:41:16.992891
Executed at 2023-04-01 23:41:23.779460
INFO:     127.0.0.1:54310 - "GET / HTTP/1.1" 200 OK
Executed at 2023-04-01 23:41:47.248950
INFO:     127.0.0.1:54311 - "GET / HTTP/1.1" 200 OK

Why does the second Start go before first Executed even though root is not async?


Solution

  • From https://fastapi.tiangolo.com/async/#path-operation-functions

    When you declare a path operation function with normal def instead of async def, it is run in an external threadpool that is then awaited, instead of being called directly (as it would block the server).

    Essentially even if you declare your endpoint function as synchronous, fast api will make it async under the hood.