1

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?

Chris
  • 18,724
  • 6
  • 46
  • 80
StuffHappens
  • 6,457
  • 13
  • 70
  • 95
  • async request handling is built into fastapi – X-_-FARZA_ D-_-X Apr 01 '23 at 22:35
  • async function defenitions are for other purposes such as updating database in some scenarios and etc... – X-_-FARZA_ D-_-X Apr 01 '23 at 22:41
  • 2
    From the [doc](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).* – Andrej Kesely Apr 01 '23 at 22:54
  • The logic for offloading sync dependencies or routes to a thread is done here for those curious: https://github.com/tiangolo/fastapi/blob/d4e85da18bb4dc475a16c20c8abad5133497e89e/fastapi/dependencies/utils.py#L614-L623 – flakes Apr 01 '23 at 23:10

1 Answers1

1

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.

Mathieu Roze
  • 123
  • 7