Async Techniques and Examples in Python Transcripts
Chapter: async and await with asyncio
Lecture: Performance improvements of producer consumer with asyncio
0:00 Let's take a moment and just appreciate
0:01 the performance improvements that we got.
0:04 And there's two metrics on which I want to measure this.
0:06 One is overall time taken
0:08 for some amount of work to be done
0:10 and the other is
0:11 what is the average latency?
0:14 How long does it take for the consumer
0:15 to get the data that the producer produced?
0:18 So, here's the synchronous version
0:19 a subset of it, we generate all the data
0:23 and then we process all the data.
0:25 And you can see the overall time
0:26 it took was about 30 seconds
0:28 and the average latency is about 15.
0:31 The best we ever got was 10 seconds
0:34 that was for the very last one that we processed.
0:37 And if we did the example where we had
0:38 two of them, it would be even worse.
0:41 So, this is okay, but it's not great.
0:44 And we saw that with asyncio
0:45 we can dramatically improve things.
0:48 So over here, we don't have
0:50 all the work being done in terms
0:51 of producing and then all the consumption
0:53 we have them interleaved because
0:55 there's all these points where
0:56 the producer's waiting on certain things
0:58 the consumer's waiting.
1:00 So that time can be more productively spent
1:02 letting the other parts run.
1:04 So we're generating some data
1:05 we're processing it, generating a few more
1:06 processing a few more, things like that.
1:09 Now, this one took a total of 21 seconds
1:12 that's about 9-10 seconds better.
1:16 Good, not rock your world better
1:18 but certainly an improvement.
1:19 But, the latency.
1:21 The latency is so much better.
1:23 Look at those numbers.
1:24 So, it's 8.5 seconds faster and 60 times less latency.
1:31 I think that's amazing. I think this is really, really great
1:33 and it was, as you saw, super easy to accomplish.
1:37 So, anytime you're waiting on I/O
1:39 files, database, webservice calls
1:42 things like that, this is really, really a great technique.