#100DaysOfWeb in Python Transcripts
Chapter: Days 21-24: Async Flask APIs with Quart
Lecture: Synchronous execution concepts
0:00 Before we start writing some code
0:01 let's just think about what synchronous execution means
0:06 and most importantly, what this looks like
0:08 and where the actual problem is in the first place.
0:11 Because, until you run into a problem
0:14 you should probably stick with synchronous code and Flask
0:16 or one of the other frameworks that we're talking about
0:18 in this course, because asynchrony does add complexity.
0:23 Let's understand the value of it
0:25 so you'll know when to use it and when not to use it.
0:28 So, here we have particular web server
0:31 and I'm trying to visualize
0:32 the processing time for various requests.
0:35 Now, I said our first example was going to be straight
0:37 async code, but let's motivate this with a web example.
0:41 So, here we have three requests
0:42 coming to our server requests, one, two, and three.
0:45 Request one, takes as long as that green box. Label that.
0:49 Request two takes about the same amount of time
0:52 another big, fat green box.
0:53 And request three is really quick, but because the way
0:56 the processing works, this just gets queued up
0:59 our server just takes them in order.
1:00 Let's do one, okay, now we're done with that.
1:02 Let's do two, now we're done with that.
1:04 Let's do three, and so on. This is not so good.
1:08 Look how short three is.
1:09 Wouldn't it be better if we could
1:10 just get that answer to them right away?
1:13 If we look at the actual response time.
1:15 So, when the request comes in, when it
1:17 they actually, as far as the external client sees it
1:20 when it's done, we can map that out as well.
1:24 So, the response gets sent back right away.
1:26 So, for request one it's, they did the request same amount
1:29 of time that it actually took us to do it
1:32 they get the response.
1:33 But request two, well, their request came in
1:36 right at that beginning of one
1:37 so they had to wait for one to be done, and then two.
1:40 And three, this tiny little thing, actually was the worst.
1:44 So, it had to wait for one and two
1:46 and then its little bit that it had to do.
1:49 This is not ideal, but if you can make the size
1:52 of those green boxes small enough
1:54 if they can outrun your traffic
1:56 you don't need asynchronous programming.
1:58 You're golden, right.
1:59 Let's suppose you are getting a request
2:01 every 200 milliseconds to your server
2:03 and it takes 20 milliseconds to process a request.
2:06 You're fine, just synchronized programming.
2:09 But when things start to back up
2:10 like this example here, well, that's a problem.
2:13 So, how are we going to deal with that?
2:15 Let's look at one more thing before we
2:17 actually get to code side of things.
2:19 We take one of those green boxes
2:20 let's say request one, and we zoom into it.
2:23 We look at actually what's happening.
2:25 There's a bunch of different things operating here.
2:28 Probably right at the top, Flask, the framework
2:31 is doing some processing, is running the routing
2:33 figuring out what to call, it's creating and
2:35 initializing the request object, things like that.
2:38 All that stuff's getting set up
2:40 and then it comes and runs our code.
2:41 The first thing our code does
2:42 let's say this is a purchase, for example.
2:46 Somebody's going to come in.
2:47 They're going to go and say, I'm user such and such.
2:50 So, our first time to do is say
2:52 well, we're going to go to the database
2:53 with their cookie, or their session, or something like that
2:56 query for their user object, and pull that back.
2:59 So, that first part, that first read database
3:02 that's us getting the user back.
3:03 Then we'd write a little code to say
3:05 well, if we have this user and they're
3:06 requesting this thing, let's find out if they own
3:09 this object, or maybe we want to show them
3:11 a list of things, or whatever.
3:12 So, we go back to the database with maybe one
3:14 or more, queries, and that runs
3:17 and then we get a response.
3:19 Maybe should be a little another blue box
3:20 where we turn our response into something
3:22 we're sending back, but then we just send that back.
3:24 The framework creates the response out of a template
3:26 or something like that, or JSON
3:28 and returns it to the request.
3:31 This is totally normal database code
3:33 and there's not a problem with it per say
3:35 however, look at this part here.
3:37 What is our code actually doing?
3:39 It's just blocked on some database query
3:42 just waiting, waiting on the network.
3:45 If we send a request over the network
3:46 to the database server, it's got it, queued it up.
3:49 Eventually it's going to compute the answer and send it back.
3:52 So, our code is just kind of sitting there
3:54 not really doing anything except for waiting.
3:58 So, the idea of the asynchronous programming is
4:00 these red sections, we can give up our execution time
4:05 and hand that off to other requests
4:07 like request two or three
4:09 while we're waiting on the database.
4:10 We don't care. We're just waiting.
4:12 So, if we could just wait and then be re-run
4:15 when the response comes back from the database
4:17 it would be the same to us.
4:19 So, that's the idea of this asynchronous of programming is.
4:21 We want to take these dead zones
4:23 these waiting zones where we're waiting
4:25 on external services, file system
4:27 database, web services, things like that
4:30 and make them productive times
4:32 for our code to do other stuff.