DEV Community

Discussion on: Asyncio, the invincible

Collapse
 
florimondmanca profile image
Florimond Manca

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 awaits, it gives control back to the event loop which allows the next task to run for a bit, until it awaits 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. :-)

Collapse
 
perigk profile image
Periklis Gkolias

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.

.

Collapse
 
florimondmanca profile image
Florimond Manca

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.