Async Techniques and Examples in Python Transcripts
Chapter: asyncio-based web frameworks
Lecture: Concept: Flask to Quart

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's review converting from Flask to Quart and going from a synchronous view or controller function into an asynchronous one.
0:09 So, if you're not entirely familiar with Flask the way it works is we have these apps. This is a Flask app that we allocate
0:15 and then we apply this decorator to set a route and it's just a URL and the data that gets passed on the URL, things like that.
0:23 Of course, you can see we have a regular def sun a regular function so there's no asynchronous happening here.
0:29 We're going to implement our viewer controller method put the logic here, and in this case we're using some other services.
0:36 We don't want to just cram all that into one place we want to be able to reuse the ability to say convert a ZIP code and country into a lat and long.
0:43 If we put those into their own services these themselves are also synchronous and what we get back is a dictionary
0:51 called sun_data, and in order to show that to the API consumer as json we have to call flask.jsonify and boom we've received a web request
1:02 we've interacted with some external services converted that into a dictionary that is converted to json and that is the heart of an API.
1:10 And this is how you do it in Flask. With that in mind, how do we do it in Quart? Well, it's very, very similar.
1:16 Again, we have the app.route, but remember the app here comes from a Quart app being created, not a Flask app. Then we add an async decorator
1:26 or async keyword, rather, for this method. So we have an async def sun and that allows us to use the await keyword.
1:33 Then, we take that same logic that we've hopefully upgraded to async capable by making get lat long and for today both async methods themselves.
1:42 And then we can await those results and this is the key to the performance that Quart is going to unlock. While we're waiting on get_lat_long
1:51 while we're waiting on for today instead of just going, well, we're busy so just process and request, don't bother us
1:59 can't do more right now, we're all blocked up. Most of the time, what does this method do but wait on those two services and adapt, you know
2:06 convert time zones a little bit? Not much, so this is really all our website does is wait on these services.
2:13 And so why don't we make that waiting productive? Do the await here and allow other async methods to run while we're awaiting these.
2:21 And that's exactly what we're doing with Quart and finally we call it quart.jsonify and quart.abort so I'm to return our data that we got back.
2:29 Once the dictionary converted to json, boom now you have a more scalable API with very little effort to make that conversion.


Talk Python's Mastodon Michael Kennedy's Mastodon