FastAPI document: Concurrency and async / await
ハンバーガー屋の例(emojiたくさん!)🍔
TL; DR
awaitを書く必要があるならasync def
awaitを書く必要がないならdef
ブロッキングIO
CPUバウンドならdefではなくasync def
If your application (somehow) doesn't have to communicate with anything else and wait for it to respond, use async def.
通信しないケース(=IOがないので、それがブロッキングかブロッキングでないかはここでは関係ない)
Very Technical Details
If you are coming from another async framework that does not work in the way described above and you are used to defining trivial compute-only path operation functions with plain def for a tiny performance gain (about 100 nanoseconds), please note that in FastAPI the effect would be quite opposite.
In these cases, it's better to use async def unless your path operation functions use code that performs blocking I/O.
「ブロッキングIOするコードを使うpath operation関数でない限り、async defを使うのがよい」