4 ms·
Almost no one really understands asyncio (I pretty much don't), but syntax like "async def" and "async with" makes you feel like you do, and stuff like FastAPI
by prionassembly 5y ago
Almost no one really understands asyncio (I pretty much don't), but syntax like "async def" and "async with" makes you feel like you do, and stuff like FastAPI encourages you to type "async".
Once in a while someone says that "yes, you can do logic programming in PHP or OOP in Haskell". Maybe, but syntax is more than a nudge, it's a finger on the scales.
- joconde 5y ago> Almost no one really understands asyncio I'd very much like a source for that, because I don't feel like it's very hard to understand. > syntax like "async def" and "async with" makes you feel like you do, and stuff like FastAPI encourages you to type "async" FastAPI's docs tell you to use "def" if you don't know what you're doing. [1] says "If you just don't know, use normal def." It's also pretty simple, I think. If your function blocks, use "def", and let FastAPI run it in a thread pool to avoid blocking the event loop. If it doesn't block, use "async", and take advantage of concurrent execution without needing a new thread. [1] https://fastapi.tiangolo.com/async/ https://fastapi.tiangolo.com/async/
- davidkhess 5y agoNot that this validates the claim, but this was a pretty influential post by the author of Flask circa 2016: https://lucumr.pocoo.org/2016/10/30/i-dont-understand-asyncio/ https://lucumr.pocoo.org/2016/10/30/i-dont-understand-asynci...
- joconde 5y agoI agree that it's kind of a mess, but it's getting better with simpler APIs (like asyncio.to_thread()), and in practice you can just use the primitives you want and ignore the rest. I haven't read the post in detail though, thanks for the link! Seems interesting.
- zippergz 5y agoFastAPI's docs tell you to use "def," yet the most basic Hello World example in the tutorial uses "async def." In fact most, if not all, of the examples in the tutorial use "async def." So I don't think it's surprising if developers new to FastAPI tend to use async by default.
- 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.
- tempest_ 5y agoFrom using FastAPI and answering some of the issues the main problem is people do not understand that python async is cooperative multi tasking and not preemptive. There are trade offs for either but I think a lot of people come to python and don't understand event loops and how they work get bitten by it.
- ascendantlogic 5y agoI just use FastAPI as a Flask replacement with Pydantic bolted on. Works out great for my use cases.