Re:FastAPI framework, high perf, easy to learn, fast to code...
by kylebarron on Monday February 01, 2021 @09:44PM
(#25996903)
Isn't the point that you should be using `def`, not `async def` here?
Re:FastAPI framework, high perf, easy to learn, fast to code...
by lukin0110 on Tuesday February 02, 2021 @04:02PM
(#26006592)
Yes. But this is a very basic example. When you have an async function defined with `await` statements in it and later on in the function you do a call to a `blocking` function you need to be aware that you have to run in the threadpool.
You don't always know that a function call is blocking, because you don't always know what is happening behind the scenes of that function and on what it depends.
def blocking(): time.sleep(5)
@app.get("/") async def index(): blocking()
The `blocking` function will blocking the event loop. This is something you need to be aware of. Gist with a few scenarios: https://gist.github.com/lukin0110/0074ec5325224674010193bb95...