Async programming is the new sexy when it comes to multitasking. It became famous with NodeJS but you can find libraries that support it in most ma...
For further actions, you may consider blocking this person and/or reporting abuse
It seems Python asynchronous programming is gaining momentum but still is quite hairy to get started with, so it's good to see this kind of quick intros popping up. Thanks. :-)
I just wanted to note that the async flow diagram you expose in your article is a bit misleading, as it is demonstrating one specific implementation of asynchronous programming — i.e. callback style async. I heard that this is how Tornado was implemented originally, but that asyncio uses a different async implementation.
From what I know, asyncio uses generators to implement coroutines, i.e. functions that can be "paused" and "resumed" at anytime. These tasks are run by the event loop (probably in a round-robin fashion), such that every time a task
await
s, it gives control back to the event loop which allows the next task to run for a bit, until itawait
s or terminates.This cooperative style is what justifies why, as you noted, async is "all-in": if any task blocks, the whole thing collapses back into a good ol' synchronous flow.
So, in this sense, asyncio is more task-based than callback-based. :-)
Thanks for making that clear, I wanted to avoid mentioning such hairy details in an introductory article. :)
As I am saying in the article,
explaining a potential async flow.
.
Sure! Async can be tough to wrap one’s head around, so it was a good idea to stick to a high-level intro here.
Coming from a C++ background where I did a bit of async programming with asio, I found asyncio hard to grasp. I stumbled upon trio, which was much closer to my mental model and vocabulary. I recommend checking it out for anyone who struggles like I did.
Indeed, trio looks really nice. In a "part 2" kindof article, I will dive into frameworks and trio will totally be in.
trio.readthedocs.io/en/latest/