I think this misses the main appeal of async. It's not about speed but about the programming model. Async makes it much easier to work with and compose concurrency compared to the thread based model. Just the fact that you can cancel tasks makes it trivial to do proper timeouts, which nobody gets right in sync applications, for example.
It also allows things like structured concurrency.
For a beautiful explanation suitable for beginners with simple examples, see Raymond Hettinger's concurrency keynote. David Beazley also has tons of talks on concurrency if you want to deep-dive.
To give you the two-minute version:
Basically, async is a cooperative model. Everybody designs their code to work together and "yield" control back to the event loop when you don't need to "be running" (like when waiting on OS calls or network for I/O -- that's the io in asyncio) so everything works in harmony. This ends up being very lighweight/efficient compared to alternatives.
By comparison, in traditional threaded models, you don't have to worry about being cooperative with your concurrent neighbors -- what gets to execute is handled mostly handled the OS thread scheduler. However, this incurs a good deal of overhead from the GIL/thread context switching for every thread. There are synchronization primitives (e.g., locks) when you need them, but they can be costly in terms of performance and those costs rise exponentially as you add more threads.
So the real benefit is not necessarily speed -- you can't crunch prime numbers faster using asyncio -- the real benefit is massive concurrency (usually in I/O-bound work, like web applications).
Threading on a single core (a-la Python's GIL) isn't fundamentally that different from async code; with async, the programmer decides when to context switch (via await), whereas in threading, the OS decides when to context switch.
The primary motivation of async programming in Python is that it is an extremely useful, modern paradigm for concurrency, allowing for better event-driven processing than threads do. From PEP 492:
We believe that the changes proposed here will help keep Python relevant and competitive in a quickly growing area of asynchronous programming, as many other languages have adopted, or are planning to adopt, similar features
21
u/rouille Sep 09 '22
I think this misses the main appeal of async. It's not about speed but about the programming model. Async makes it much easier to work with and compose concurrency compared to the thread based model. Just the fact that you can cancel tasks makes it trivial to do proper timeouts, which nobody gets right in sync applications, for example.
It also allows things like structured concurrency.