Async Techniques and Examples in Python Transcripts
Chapter: Why async?
Lecture: Concept: Visualizing an asynchronous request

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's look at this same series of requests but with our web server having the ability to execute them concurrently at least while it's waiting.
0:10 So now we have request 1 come in and it's... we can't make that green bar get any shorter that's how long it takes.
0:15 But, we saw that we were waiting before and if we could somehow run request 2 while we're waiting and then request 3 while both of those are waiting
0:23 we could get much better response times. So if we look at our response time again we have... 1 takes exactly as long as it did before
0:31 cause it was the first one, it was lucky. But it takes as long as it takes. However, request 2 is quite a bit shorter.
0:39 Didn't have to wait for that other processing we're able to execute it concurrently. request 3 is the real winner here.
0:45 Actually, it returned much, much sooner. It takes as long as it takes to process it instead of five times.
0:53 Now we can't possibly get perfect concurrency, right? If we could do every single one of them concurrently then there'd be no need to have anything
1:00 other than a five dollar server to run YouTube or something. There is some overhead. There are bits of R Code and framework code
1:07 that don't run concurrently. But, because what our site was doing is mostly waiting we just found ways to be more productive about that wait time.
1:16 Zooming in again, now we have exactly the same operation. Exactly the same thing happening but then we've changed this code.
1:25 We've changed ever so slightly how we're calling the database and we've freed up our Python thread of execution or just the execution of our code.
1:35 Let's not tie to threads just yet. So that we can do other stuff. So request 2 came in, right during that first database operation
1:43 we're able to get right to it. By the time we got to our code section request 2 had begun waiting so then we just ran our code, we went back to waiting
1:51 request 3 came in, and we just processed that because both of the others were waiting and so on. So we can leverage this wait time
1:58 to really ramp up the scalability. How do we do it? Well, it's probably using something called AsyncIO which we're going to talk about right away.
2:06 It may even be using Python threads but it's probably AsyncIO. If you don't know what either of those are
2:13 that's fine, we're going to talk about them soon.


Talk Python's Mastodon Michael Kennedy's Mastodon