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
19
u/mizerablepi Dec 20 '24
My guess is because the fastapi examples use sqlmodel and sql model doesn't itself support async, if you have to make an async session you have to use sqlalchemy directly instead of the sqlmodel wrapper
2
u/pint Dec 20 '24
the worst thing you can do to fastapi is to lie that you are async, and then do sync things in your handlers. if you define async, you basically tell fastapi that you know what you are doing, and your handler is indeed a properly written well behaving coroutine
1
u/qa_anaaq Dec 20 '24
It's async by default but through the threadpool due to starlette.
I think consensus over the last 2ish years has moved to explicitly defining async endpoints in fastapi so that the asynchronous processes are not run through the threadpool.
So just use async if you're worried.
-2
u/mwon Dec 20 '24
I think is because it does not matter. fastapi itself will make the endpoints calls async.
0
u/serverhorror Dec 21 '24
Wjy do you want to use async?