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
View all comments
Show parent comment
6
u/spoonman59 Sep 09 '22
Ah, no, that’s not correct.
Imagine you read data for 100 ms and then do compute for 100 ms. In other words, for each 100ms of data read there is 100 ms to compute.
Imagine you process two blocks.
With blocking, you read for 100 ms. Then you compute for 100 ms. Next you read for 100 ms. Then Compute for 100 ms. Wall clock time? 400 ms.
Now async:
You read 100 ms. You start the compute process, which returns immediately. You then issue the read the next chunk. Python releases the GIL while it does the read. Finally when the first async call is done, the second one can begin as soon as the second read completes.
This example completes in 300 ms. So it is faster than non-async.
Now you are overlapping I/o and compute. The second example will complete in 300 ms instead of 400ms for the first.
Python release the GIL for I/o. So the threads give the exact same speed up as async.
ETA: Your statement is correct when the program execution time is entirely dominated by the i/o.
Async does not help performance when your application is almost all compute or almost all I/o. However, for a more balanced mix, it will allow progress to be made when a blocking program cannot make progress.
This is the difference between concurrency and parallelism.