#100DaysOfWeb in Python Transcripts
Chapter: Days 21-24: Async Flask APIs with Quart
Lecture: Adding an async view method / controller method

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So let's make this async
0:01 and we'll begin by starting at the lowest level
0:04 so, in order to make this useful, we have to make this
0:08 an async method, and that an async method
0:10 so we can await it here. So let's go here
0:13 well, there's nothing we can await here, right.
0:16 This is regular request, we can't await that.
0:19 So, instead we have to switch to using a new API.
0:23 Now, it's going to look a little bit funky but it's
0:25 it's not too bad.
0:26 So we're going to get rid of this and we're going to say
0:28 aiohttp and instead of this part coming out
0:32 I'm going to delete it in a second
0:34 we're going to come over here and use a with block
0:37 a with block to manage the life cycle of our request
0:41 and that with block itself is async
0:43 so the syntax is a little wild, let's say.
0:47 So, we create an async with block
0:49 for an aiohttp client session, add session.
0:55 Cool, right? Now, we're going to use that session to make a call.
1:02 Get that URL and we're going to store the response
1:05 well, as a response. Then we're going to go like this.
1:10 Now, this should get begins the request
1:13 this one finishes it and that also is an async method
1:18 so we need to go over here and add an await there.
1:20 Now, for any of this to work we need this to be an async
1:24 an async method up here.
1:26 Okay, so, we could also do response.raise_for_status
1:33 like that. Intellisense autocomplete thing doesn't come up
1:37 but it's good. The other thing to do is
1:41 if we switch this caching on for this to work
1:43 this has to be awaiting an asyncio.sleep
1:50 otherwise it's not it doesn't simulate the right kind of thing.
1:54 Okay, so we had that now we have this
1:57 it's slightly more complicated but
1:59 well, honestly what we have is that right there, right?
2:03 It's a little bit more complicated but honestly
2:05 not so much. Let's do the same for the sun.
2:09 Async the method. Await asyncio for that one
2:19 that's not really what we're doing but here
2:23 these are aiohttp and we'll just do it like that.
2:27 Once we've turned this into a Python dictionary
2:30 we no longer need to be in here like this
2:33 and this of course, we have to await that right there.
2:37 Let's call that data and then, let's say sun.
2:45 Perfect. So, that's going to give us our items back
2:48 we can go do the processing
2:49 all the stuff right here is happening
2:52 outside the concurrency.
2:54 In fact, we could move that back, I suppose.
2:57 Like that. Okay, we've now made this async.
3:01 Let's run it see if it's working, still.
3:04 Oh, got to tell this to run single instance.
3:08 So if we start calling this on one port
3:12 well that part still works
3:13 but we didn't change anything there.
3:15 This one, though
3:16 this one we've made some changes
3:17 actually, I think it's going into that one, let's see.
3:20 Yes, bad bad trouble. What is wrong here?
3:23 Well, I'll show you what's wrong. We didn't finish.
3:26 We've made those async methods
3:29 but we have not made this actual method use them correctly.
3:33 Because they're async
3:34 they have to be called from async methods
3:37 and we have to await them to get their response.
3:40 Now, let's try. Golden. Golden. Look at that.
3:46 Now, it looks the same.
3:48 Any individual request will take
3:50 about the same amount of time
3:52 but instead over here, let's just jump to this one.
3:56 Instead of blocking and just going, well
3:58 we're awaiting on this until we can do anything else
4:01 we're now going and saying we're awaiting
4:05 all of these things.
4:06 We're awaiting getting the session the URL
4:09 reading the stream to get the JSON
4:11 all that kind of stuff.
4:12 So that means that all of these steps through here
4:15 other processes
4:17 other requests can come along and do the processing
4:20 just like I showed you with the more obvious
4:23 producer consumer version same thing with the web here.
4:26 So we're sharing
4:27 basically giving up our wait time to go do other work
4:30 let other requests be processed. That's awesome.
4:34 And how hard was this?
4:35 Well, you find and replace the word Flask with Quart
4:39 and then you write async in this
4:41 programming code here. For web services, you have this.
4:45 For time, you have this.
4:46 I don't know why you'd ever do this in a real web app
4:49 but this simulates some load balancing
4:51 or some load testing
4:52 we needed because this eventually gives up
4:54 if we pound it too hard. Pretty straightforward.
4:57 And then you have to do it all the way
4:58 up and down the stack
4:59 so the most important part is the outer side
5:01 outer, outer piece
5:03 we have async methods and we await the results
5:05 when we call them. Basically, that's it.
5:09 That's how you get going with Quart