MongoDB with Async Python Transcripts
Chapter: Foundations: async
Lecture: Skeleton async Program

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Are you ready to write some async code using async and await? Let's do it. Let's do it.
0:07 So we're going to go back to an API call, not a database call just yet. Because we haven't talked about how to do the database work yet.
0:16 We haven't talked about beanie and API's are perfect. Even if the API itself for single threaded single level concurrency, the internet itself
0:27 is not and a lot of times what you're waiting on is actually the messages making its way through the internet pipes, not the actual server itself.
0:36 So back to our weather service. Notice that we can go up here and we can say we want to go to a city like city equals Portland
0:44 state equals Oregon, we could go Seattle and Washington, we would get Seattle Washington's
0:52 report which is pretty similar here they're geographically close. So I want to do that for a bunch of places.
0:59 So I actually went and asked ChatGPT, hey, we've got this URL like this, please generate a bunch of URLs for cities, it did.
1:07 And so now we're going to go and call these first in a serial way, and then in a concurrent asyncio way.
1:17 So I've already typed those in just to save you the trouble of generating those and writing them down all of that. Now let's write a function.
1:25 I'll start out, I guess, this one, I guess we could have it be synchronous, but all of its work it's gonna do is going to have to be asynchronous.
1:34 And there's a little bit of a weird twist that you'll have to get your mind around to make this work. So for now, I'll just say, ""Hello, weather,
1:44 and make sure we're running it. Notice it's not running, right? Why? Because we haven't done our dunder. You know, if dunder name,
1:52 This, and in fact, I'm tired of writing that and you'll be tired of watching me write it.
2:02 So let's go over here to the live templates, go to Python here, and we're going to add one of these real quick. Add a live template.
2:18 it main, like that. I'm going to define the context, which is anywhere in Python, just to keep it simple.
2:28 Now if we go over here and we just type main tab, boom. I don't have to write that again. Cool, cool. Now what we want to do is we want to call these.
2:39 You might think, ""Okay, call an API, requests. No, no, no, no, not requests.
2:45 does not have an async API. But you know what does something very similar HTTP x, the next
2:53 generation of Python client primarily because it lets us write code up here like this. Well,
3:01 this one synchronous, but we can do an async version as well. Here like this, where we
3:09 we create an async client. So copy that for a second. And let's go write some code. In
3:15 order to do asynchronous programming, we need a function that can run asynchronously. So
3:21 you might write def get report given, given a URL. And then we would do something like
3:30 this, right where we're going to say async with and then a weight client. Well, we don't
3:37 have our HTTPS yet. So let's go and add that to the requirements here. And then let's regenerate
3:45 the requirements.txt and install it. Notice a lot more things came in. And over here now
3:59 we've got all these dependencies, which why is that one there? Well, because of those those two reasons. Excellent, so now it's just a matter
4:07 of importing that at the top. Now we can await client get URL while the response like this. So by default, what we get if we call this
4:19 is the return value. Does it say here? Yes, and can I get to it without making a go away? Yeah, here we go.
4:25 This is a coroutine that returns a response. So the type of this is a coroutine that has not yet started
4:33 order to run it, we need to tell Python, like we had those colored slices in the event queue, we need to say here's a slice.
4:42 So we're going to now block, you can go do other work, dear Python, while this is running while we're talking to the internet.
4:51 When it comes back, please run the code from here on out. One of those is raised for status.
4:57 So if there's an error talking to the server, like a 404 or a 500 or timeout, whatever, we'll make sure we don't keep going.
5:08 And then we should be done with this, we can say return, not JSON. So we can parse this as JSON. And this here for now is going to be a dictionary.
5:22 It could be, and maybe we'll make it, it could be this class up here. Remember this? We'll see about that.
5:33 For now, let's just return this just to make sure that we can get something.
5:37 So what we want to do is we want to say, let's just take the first URL, and we'll say report. We want to call get report and pass in locations.
5:50 Let's just do a zero for now. Well, look at this error here. It says a couple problems along the lines. Don't worry about that.
5:59 I don't see PyCharm give us an error. But this for sure is going to be a problem if we try to run it.
6:05 For example, async used outside of an async function. So normal functions can't be async.
6:13 So anytime you're calling, you're using the async or await keyword that can only be done within the context of an async function.
6:24 Now if we run it, we get a warning. But what happened is this generated a co routine, but it didn't call anything on the server.
6:32 I'll put like right here, I'll say, print contacting URL. You don't see that.
6:43 because that function never ran, it got created with the potential of running, but we never awaited it.
6:51 So what we need to do, let's go ahead and make this async_def. We can await this. And then we need to run this here.
7:03 So the way we're going to run that is we say async_io, import that, dot run, that coroutine. And it ran, let's go ahead and print report.
7:20 Let's just do the forecast. There you go. Look at that. Well, where did we go ahead and print? Print that out so we can see.
7:36 We're calling New York City. Okay, and we got that. I guess we could also print out the location. Let's see. It is location. New York City right there.
7:49 You can see that that got us the right one back. So that's how it works with async and await. We're going to create an async function
7:57 using async def instead of a regular one, which then allows us to use async with or await. And then in order to run it,
8:08 It has to either be called from within another async function and awaited, or you can run it as the top level async thing using asyncio.run. Excellent.
8:20 So that's the first part. Now what do we do to call all of these locations at the same time?


Talk Python's Mastodon Michael Kennedy's Mastodon