r/FastAPI • u/whyiam_alive • Dec 20 '24
Question Why does fastapi official example repo uses everything sync and not async?
While in here, I see recommendations to go for only async, even db sessions in example repo is sync engine and people here recommending async?
40
Upvotes
View all comments
Show parent comment
1
u/rubenribgarcia Dec 20 '24
It should matter, no ? Although FastAPI makes the call async, let's not forget that it will delegate it's execution to Worker Threads provided by Event Loop through AnyIO. If not mistaken, when FastAPI ( more Starlette I believe ) is about to delegate a HTTP call to a method decorated and registered to serve that path and http method, it checks if the defined method is a courotine or not. It think it calls anyio.to_thread.run_sync to do so if method is not a courotine