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


Talk Python's Mastodon Michael Kennedy's Mastodon