Async Techniques and Examples in Python Transcripts
Chapter: asyncio-based web frameworks
Lecture: Demo: Introducing our Flask API

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's talk about the web app we're going to build.
0:02 It's already created as a Flask app.
0:05 It takes a while to build
0:06 and I don't want to to spend all the time doing that
0:08 just diggin' into Flask. It's not a course about Flask per se.
0:12 But we're going to take a Flask app and we're going to
0:15 make it asynchronous and we're going to implement
0:17 all of the things you have to do to go
0:18 from the traditional synchronous style to asynchronous
0:21 which is going to be pretty awesome.
0:23 So I want to introduce you to it and then
0:26 we'll start programming the async transformation
0:28 it's going to go through.
0:29 So there's actually two web apps here
0:32 Cityscape API and ACityscape API.
0:35 Right now these are both the original serial version.
0:39 This one we're going to transform
0:42 into the asynchronous version.
0:44 So I'm going to leave that there for you to have as a record.
0:47 This is the one we're going to work on.
0:49 So let's just go through really quick.
0:50 Now I don't assume that you know Flask like I said but
0:54 we're not going to go into too much of the details.
0:56 You'll see enough that you'll be able to take it away
0:58 and if you learn a little bit about Flask
1:00 then you'll be able to adapt this, no problem.
1:02 Maybe the best way to introduce you to it
1:04 is to actually see it running
1:05 so let's start it real quick, open it up.
1:08 And it says we can use this API, sun/zipcode/country
1:12 is one of the APIs so let's put that in here.
1:16 So zip code, might be 97227 if you could type the 9.
1:22 And the country code would be something like US
1:25 or your country and you can put your postal code in there.
1:27 And you hit this, and what we get
1:30 see it took a second 'cause it really went out
1:32 to the Internet to another service
1:33 a couple services actually
1:35 and it figured out what sunrise and sunset
1:38 in and around Oregon is.
1:40 I don't know exactly where that is, but which
1:42 where this zipcode is but it's pretty close to me
1:44 and this looks about right.
1:46 So sunrise at 6:48 a.m., sunset 7:30 p.m.
1:50 We can also ask for the weather.
1:52 It'll give us all sorts of details back about the weather
1:55 how much cloudiness there is.
1:58 What's the humidity and pressure.
2:00 The moderate rain apparently is something happening
2:02 right there so, pretty cool.
2:05 So we can basically ask for sunrise, sunset
2:08 and we can ask for weather. That's what this API does.
2:12 Now let's see how it goes about doing it.
2:15 So this, this runs when everything starts up.
2:19 This creates our Flask app so here's our Flask app.
2:22 And we've used this concept called a blueprint
2:25 a Flask blueprint, to take the sort of HTML view stuff
2:30 and put it in one file
2:31 and the API bits and put it to another file.
2:34 So it let's you separate our apps.
2:36 I really like that pattern.
2:37 And then it just goes and it sets some values
2:39 out of config files that live over here.
2:43 Then it calls run.
2:44 So that's pretty straightforward.
2:45 Let's just look at the API bit over here.
2:48 So for example let's look at the sun
2:50 it's more complicated.
2:51 So you give in a zipcode and the country as strings
2:55 and we have to call this function
2:56 to actually get the latitude and longitude.
2:59 That is actually what's required
3:01 for the sun_service that we use say
3:02 given any spot on the globe
3:04 what is the sunrise, sunset information?
3:07 So these services right here this one, and this one
3:13 those are implemented over in the service section
3:16 so we have the location_service for example.
3:18 Sun_service for example.
3:22 So I'm going to hide away this cached data
3:24 'cause we're not using it right now.
3:25 This is only if you want to do performance testing
3:27 we'll talk about that later.
3:29 So we're going to go in and we're going to use requests
3:31 we're going to go and just get a call
3:33 and we're just going to do some json parsing here.
3:35 And then we have to convert from UTC to local time
3:38 just so things make sense to you
3:40 in that location when you look at it okay?
3:45 So this is it.
3:47 How are we going to add asynchronous into this?
3:49 Well this function right here
3:53 could be async theoretically, except for
3:56 well see, Flask does not support any async capabilities
4:00 whatsoever, at all.
4:03 We'll talk more about that in a moment.
4:05 There's actually an interesting piece of the documentation
4:07 and some interesting news around that as well
4:09 and depending when you watch this
4:11 that may or may not be still true
4:13 but Fall 2018 Flask definitely does not support
4:16 any form of async. So we're going to somehow find a way
4:19 to make this function async
4:21 and then we'll be able to implement that
4:23 in aiohttp client style
4:25 as we did before, same for this.
4:27 And then we can await those and instead of having
4:29 this method just block while we're talkin' to that service
4:31 block while we're talkin' to that service
4:33 both of those times, when they're blocked
4:36 will let other requests come in
4:38 and maybe process the weather request.
4:39 Or a different send request, things like that.
4:42 This is our app that we're going to start working with
4:44 and this is the app we're going to use for our demo.
4:46 Like I said this one is hangin' around for you
4:49 to just have as a record.
4:51 This one right now is synchronous
4:53 but it's going to be converted to
4:55 an async enabled, Flask-like, Flask-based API
4:59 but it won't actually be a Flask.