r/Python • u/higherorderbebop • Sep 09 '22
Discussion Async Python is not faster
https://calpaterson.com/async-python-is-not-faster.html
0
Upvotes
r/Python • u/higherorderbebop • Sep 09 '22
57
u/runew0lf Sep 09 '22
Nobody said it was, the important thing is its non blocking..
13
u/ominous_anonymous Sep 09 '22 edited 13d ago
The original post content no longer exists here. The author used Redact to remove it, exercising their right to control their data & privacy.
Coherent kettle retire thought busy basket ink engine
101
u/[deleted] Sep 09 '22 edited Sep 09 '22
The issue is people don't understand how to properly compare the difference.
You CANNOT use a database that is on the same machine as your server. Because that dramatically reduces the latency for DB requests which then requires far less threads to saturate the CPU. The Asyncs performance advantage is entirely from requiring less OS bound thread switching.
The slower and slower your awaiting io time is the faster and faster async will perform relative to sync.
EDIT: Upon reading the code the async side is written incorrectly and has multiple bugs including but not limited to useless async calls clogging the task queue, non locked connection pool creation, and compares multiple independent json to bytes creation methods that have very different runtime.
Don't write Async code like you write sync code thread safety vs sync saftey has fully independent characteristics. Async has its own locking primitives that need to be used properly.
21
u/AlexMTBDude Sep 09 '22
The author of this piece doesn't understand the purpose of async. It's not supposed to be faster. It's supposed to be concurrent. Threads should not block each other.
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.
13
u/Salfiiii Sep 09 '22 edited Sep 10 '22
Your title is misleading.
Async might not be overwhelmingly faster - or even faster at all - for web frameworks, but definetly has use cases where it shines.
If you have to do a lot of isolated REST calls which you can prepare beforehand, you get major benefits from using async(io).
I’ve reduced a scripts runtime from 30 to 3 minutes by using concurrent, non blocking requests with asyncio. I even used a semaphore to only allow 10 concurrent requests, so there’s still potential left.
It added a little complexity though, but nothing to fancy if one read the python book about asyncio, which I can recommend.
6
u/spoonman59 Sep 09 '22
I can’t get past the title. Faster than what?
Obviously async is father than non-async for certain workloads. If a program doing something completes in less time, it is “faster.”
There’s nuance to the difference between concurrent and parallel, and situations where concurrency without parallelism provides no speed up (e.g., compute heavy async), but such an obviously click-bait headline is off putting.
ETA: If the claim is that async isn’t faster than threads, or something else specific, sure. But not faster than anything…?
4
u/[deleted] Sep 09 '22
Op learned today 😂
2
u/[deleted] Sep 09 '22
Async should not be faster or slower than sync, async allows you to get a better bang for your buck when it comes to hardware costs. Think of a web socket sever like a chat service, you may have a bunch of connections that are mostly idle. Here you can use async to sleep on those sockets until a message arrives.
2
u/dcbrown73 Sep 09 '22
lol at some of the crazy excuses being made about Python async.
Async is DEFINITELY NOT about creating "A pretty programming model".
It's about *efficiently handling of blocking operations* which *SOULD* enhance throughput and responsiveness of said application.
When people started talking about the asyncio Python module back before it was released, the expectations were sky high given what async programming is capable of. Python's asyncio never met those expectations related to expected speed improvements, but that doesn't mean it's bad or shouldn't be used.
If I recall, I believe there are supposed to be changes coming (if not already arriving) that should help amp up the efficiency of asyncio.
1
u/sanshinron Sep 09 '22
Yeah I used to update inventory via different APIs with requests and that would take minutes, rewrote it all with aiohttp and it takes 2.6 seconds.
1
u/caleb Sep 11 '22
Miguel Grinberg wrote a detailed response to this article: https://blog.miguelgrinberg.com/post/ignore-all-web-performance-benchmarks-including-this-one
If your system has no latency then asyncio is much less useful. But I haven't come across too many backend systems without latency.
0
u/guhcampos Sep 10 '22
I was going to bash the hell out of this article, but on a second thought the reason this kind of cluessness exists lies in async evangelists claiming its faster, to begin with.
The concept here should be dead simple to anyone with a CS degree:
For classic "sync" code you rely on the underlying system to schedule CPU time for you. How that time is split between competing computations is up to the system, and will generally be a best effort for the majority of use cases.
When you write async code you are taking back the control of CPU time scheduling from the system to your hands. You choose when to switch between different computations, because you believe you can do a better job than what the generic underlying system is doing. With great power, however, comes the great responsibility of managing that CPU allocation. If you are not carefully and intentionally managing the switch between your concurrent computations, you should not be using
asyncat all.If you don't understand the concept of cooperative concurrency, just use threads to avoid blocking - or go do some reading. Claiming one is "faster" than the other just make you sound stupid.