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