#100DaysOfWeb in Python Transcripts
Chapter: Days 21-24: Async Flask APIs with Quart
Lecture: Asynchronous execution concepts
0:00 Let's look at the order and timing of operations.
0:03 Imagine we have asynchronous execution like we just saw.
0:07 So again, we have request one, two and three
0:09 coming in on exactly the same timing.
0:12 They take exactly the same amount of time to run.
0:16 But when we dug into the requests
0:18 we saw most of the time
0:19 we're waiting on a database or something else right
0:21 like a web service.
0:22 So during those times, maybe our requests
0:25 actually could just run almost truly in parallel.
0:29 Notice, for example, when request three comes in
0:31 we couldn't quite run it
0:32 exactly when that request came in
0:34 but it's pretty close
0:35 because soon request one and two
0:38 went back to waiting on the database.
0:39 So again, let's look at the timing here.
0:42 So, we have our request one come in.
0:44 And, of course, just like before
0:45 it got done right when it did
0:47 in fact maybe just took a hair longer
0:49 cause it gave up its time
0:51 and couldn't quite get it back from two
0:53 right when it was ready to run, somethin like that.
0:56 But we have our response time
0:57 roughly the length of request one.
1:00 But now, when request two comes in
1:02 we saw that request one was waiting on the database
1:04 so it could immediately start running.
1:06 And it got done more or less
1:07 in the same amount of time it took.
1:09 And request three was, actually its response
1:12 was moved up ahead of request two.
1:15 So from a response perspective, it went one, three, two
1:18 because three was really short and one and two
1:21 were waiting on the database.
1:22 So, boom one, two, three
1:24 and it could just get that out of the way.
1:25 So the response time per request from the outside
1:28 one, two, and three, looked much more like
1:31 what they would be
1:32 if there was just one user to the site
1:34 even though it was theoretically really busy.
1:37 Now if we dig in again like we did before
1:39 we'll see that yes, we're still doing framework database
1:42 code, maybe web services which I haven't highlighted here.
1:45 But now the database section is green
1:47 because during those times, we're telling Python
1:50 via our async and await programming
1:53 you know what? I'm just waiting here.
1:55 If you've got other stuff to do
1:56 other requests or processes
1:58 feel free to go work on them until they start to wait.
2:00 And then let me come back and run my code.
2:02 That's the beauty of async programming.
2:04 This is a really powerful concept in Python in general.
2:09 And it happens to be really useful on the web
2:10 because a lot of time, what you're doing on the website
2:13 is waiting on a database, waiting on an external service
2:16 something like that. You can make that productive waiting time.
2:20 So that's an overview of asynchronous programming in Python.