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

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

Talk Python's Mastodon Michael Kennedy's Mastodon