Async Techniques and Examples in Python Transcripts
Chapter: Why async?
Lecture: Concept: Visualizing an asynchronous request
0:00 Let's look at this same series of requests
0:03 but with our web server having the ability
0:05 to execute them concurrently
0:07 at least while it's waiting.
0:09 So now we have request 1 come in
0:10 and it's... we can't make that green bar get any shorter
0:13 that's how long it takes.
0:14 But, we saw that we were waiting before
0:17 and if we could somehow run request 2 while we're waiting
0:20 and then request 3 while both of those are waiting
0:22 we could get much better response times.
0:24 So if we look at our response time again
0:26 we have... 1 takes exactly as long as it did before
0:30 cause it was the first one, it was lucky.
0:32 But it takes as long as it takes.
0:34 However, request 2 is quite a bit shorter.
0:38 Didn't have to wait for that other processing
0:40 we're able to execute it concurrently.
0:41 request 3 is the real winner here.
0:44 Actually, it returned much, much sooner.
0:47 It takes as long as it takes to process it
0:50 instead of five times.
0:52 Now we can't possibly get perfect concurrency, right?
0:55 If we could do every single one of them concurrently
0:58 then there'd be no need to have anything
0:59 other than a five dollar server to run YouTube or something.
1:02 There is some overhead.
1:04 There are bits of R Code and framework code
1:06 that don't run concurrently.
1:07 But, because what our site was doing is mostly waiting
1:11 we just found ways to be more productive
1:13 about that wait time.
1:15 Zooming in again, now we have exactly the same operation.
1:19 Exactly the same thing happening
1:21 but then we've changed this code.
1:24 We've changed ever so slightly
1:25 how we're calling the database
1:27 and we've freed up our Python thread of execution
1:31 or just the execution of our code.
1:34 Let's not tie to threads just yet.
1:36 So that we can do other stuff.
1:39 So request 2 came in, right
1:40 during that first database operation
1:42 we're able to get right to it.
1:44 By the time we got to our code section
1:45 request 2 had begun waiting
1:48 so then we just ran our code, we went back to waiting
1:50 request 3 came in, and we just processed that
1:52 because both of the others were waiting and so on.
1:54 So we can leverage this wait time
1:57 to really ramp up the scalability. How do we do it?
2:00 Well, it's probably using something called AsyncIO
2:03 which we're going to talk about right away.
2:05 It may even be using Python threads
2:08 but it's probably AsyncIO.
2:10 If you don't know what either of those are
2:12 that's fine, we're going to talk about them soon.