3 ms·
> FastAPI's docs tell you to use "def," yet the most basic Hello World example in the tutorial uses "async def." Because that's the correct way. print("hello,
by joconde 5y ago
> FastAPI's docs tell you to use "def," yet the most basic Hello World example in the tutorial uses "async def."
Because that's the correct way. print("hello, world!") will block for a very short time, so it's not worth sending to a thread pool.
> In fact most, if not all, of the examples in the tutorial use "async def."
That's again because they show the correct way to use async.
Using async correctly, when it applies is preferable to a synchronous function. The docs encourage you to do it if you know how, and if your use case fits an async function.
> So I don't think it's surprising if developers new to FastAPI tend to use async by default.
It is to me, because the docs are clear:
- if you know your function can be made async properly, make it so,
- if your function blocks, use "def",
- if you don't know enough to choose, use "def".
People who complain that the event loop blocks too often simply haven't read the docs well enough.