Async Techniques and Examples in Python Transcripts
Chapter: asyncio-based web frameworks
Lecture: Demo: Making our API async
0:00 Now that we've converted our framework from Flask to Quart
0:03 everything's working but that's not really
0:05 any async magic right?
0:07 These methods are not async def
0:09 they're just def regular methods.
0:11 And most importantly the reason they aren't is
0:13 we can't await this, we can't await that.
0:16 There's no reason for them to be async
0:17 because they don't do async stuff.
0:20 So we got to start at the lowest level to enable this
0:22 so let's go down to this location_service.
0:24 And here we're using requests but remember
0:27 the way that we actually do this for async
0:30 is we do this with aiohttp client.
0:33 If you didn't get that check out the very first chapter
0:35 where we talk about asyncio in case you're skipping around.
0:39 How does this work? Well first of all
0:41 we have to have that as a requirement here
0:44 so we're not going to have requests any more
0:46 we're going to have aiohttp. Now that's fine on its own
0:50 but we also want to add things like aiodns and cchar.det
0:56 for better performance there.
0:59 PyCharm thinks these are misspelled
1:00 just tell it it's not.
1:02 Alright once again let's run our requirements install
1:06 'cause we now have new requirements.
1:10 Excellent, everything was installed successfully.
1:13 So with that out of the way
1:15 we can now reimplement this using aiohttp client.
1:18 So we'll say async with.
1:23 Import that, aiohttp.ClientSession() as session
1:27 and then async with session.geturl() as response.
1:34 So we don't do that line and those need to indent
1:38 and we're almost there. Now notice there's a little hint
1:41 that something's not quite right.
1:42 Look at this get here.
1:44 Coroutine does not have a method called get.
1:47 Well that's not what we wanted.
1:49 Previously this was the dictionary
1:50 and it was a return value that.
1:51 Just like in the previous section we have to await
1:55 maybe not that much await.
1:56 Await that, there we go.
1:58 Now what we get back is a dictionary and we're good to go.
2:02 So this almost works and the reason we didn't get
2:05 a little more help from PyCharm there was
2:07 we hadn't yet said this an async method itself.
2:11 Alright so this looks pretty good.
2:13 Let's just go and do it up here.
2:14 Actually, that's the only one in this service.
2:18 Let's go do the next service.
2:20 The other one we have to work with is our sun_service
2:23 and again this is going to be aiohttp.
2:26 We get this stuff happening. We can indent it
2:31 so that this can come out of the async part.
2:34 And this we need to make into a little variable
2:39 that we can await to get it and then we can call get.
2:43 Perfect so, unless I mess up the indentation somewhere
2:47 this should work. Oh and async.
2:53 That's fine, that's just CPU bound.
2:55 Alright it looks like this might be working.
2:57 The final step would be to actually test it here.
3:01 We haven't tested if we try to call this function
3:03 it's going to get, it's not going to work right?
3:05 Because this is not two values you can unproject
3:08 it's a coroutine. So final step to make this all work
3:14 we come down here we can go to this and just double check.
3:17 Yes that's an async method even though PyCharm
3:19 is not helping us we have to await
3:21 to get the two values back so we can project them into this
3:24 yeah unpack them similarly here.
3:27 That's it, we have an end-to-end from top-to-bottom
3:30 bottom-top, how you like it. Async method, so async view.
3:35 We await the various operations
3:36 each of those are async methods that can be awaited
3:40 using things that we're already familiar with
3:41 like aiohttp client. Now the big test.
3:44 After all this craziness, does this still work?
3:46 Let's find out.
3:47 So the thing we changed was the sun API.
3:53 Let's try this one. Boom look at that.
3:56 Now you probably won't see any performance difference.
4:00 Remember those big green boxes and the response time?
4:03 The green boxes didn't get shorter
4:05 they took exactly the same amount of time.
4:07 We're still waiting for those two services to do their thing
4:11 but the key difference is
4:12 the thread that runs the web server
4:15 is now free to take any other request
4:17 and work on it while we're waiting.
4:19 Previously they were just blocked.
4:21 But now it's golden
4:22 it can just let everything go and it's really beautiful.
4:26 The fact that this is still working means that
4:28 now if we start testing it for load
4:31 do some sort of load testing or scalability tests
4:33 we should see better results.
4:35 With a few caveats that we're going to get to in just a minute.