Modern APIs with FastAPI and Python Transcripts
Chapter: Building a realistic API
Lecture: Making an async API method
0:00 We're not taking advantage of one of the real killer features here in FastAPI.
0:05 we can allow our Web servers code do other work while it's waiting and how much
0:11 work you think we're actually doing in our code versus just waiting on getting a response
0:14 from this weather service. It's like 99% waiting.
0:18 If that random guess is true and I'm sure that it's close,
0:22 we'll be able to get like
0:23 100 times the scalability by making this use async and await,
0:27 because when we're waiting on other stuff,
0:29 we could be handling other similar requests.
0:31 However, in order for us to do that,
0:33 this method, this get report, has to be async,
0:37 and we might even want to go so far as to give it a suffix, that
0:40 it's an async method, I don't know,
0:43 and if you haven't worked with this before,
0:44 we did touch just a bit on it,
0:46 but we didn't start from scratch,
0:47 what you do is you say this is an async method here,
0:50 and then you have to, where you're waiting,
0:53 say the word await. Great. Wait a minute.
0:56 This is not working. You cannot await on response.
1:01 You have to wait on a co-routine. Request
1:03 doesn't support this. So what we want is httpx. And let's go back over
1:07 here and put, we already got
1:09 httpx, but what we don't want is we don't have requests because we don't
1:13 need it. So instead, we're gonna do this, we'll say "async with
1:18 httpx async client as client",
1:22 and then we do these things.
1:24 So we got a response where we go the client and we do get URL.
1:29 That's it. That's all that changes.
1:32 And we got 100 times scalability. Killer,
1:35 right? Okay, again, that's a super rough estimate,
1:37 but saying that we're waiting, you know,
1:39 like the ping time is 100 milliseconds.
1:41 It takes us one millisecond to do the processing.
1:43 That would be true. So we're gonna start working with this client,
1:47 and then we're going to go and say,
1:50 get the URL and we just go from there.
1:52 However, what happens when we run it?
1:54 It's probably going to hate this.
1:55 Let's try. We go over here.
1:57 What did we try to do?
2:00 Well, we returned what we thought was that here.
2:04 But if we actually print out and report,
2:07 it's not a dictionary anymore, as you will see,
2:09 it is, we go to the top,
2:11 It is a co-routine. And unless we don't get the value of
2:15 the information out of these asynchronous methods,
2:17 unless we await them. Okay,
2:19 so, go back and do our await here.
2:22 Fantastic. Except even though PyCharm doesn't say it,
2:25 we can't actually await stuff if we're not in an async method.
2:30 There you go. So in order to use this async method,
2:32 we have to await it and make sure the route is async.
2:35 And then internally for this to mean anything we have to use
2:38 some async libraries right there.
2:40 That's literally, like, three to five lines of code change.
2:44 Super, Super small. Try it again. It works like a champ,
2:48 But you know what? We can do a ton of requests while we're waiting on
2:52 that information to come back, so super, super neat that we could do this.
2:56 What's nice is you don't have to worry about async event loops or how that
3:00 happens or awaiting things properly. All you have to do is say your API
3:04 method is async and then leverage async
3:07 things so we're leveraging async
3:09 API calls. If you're working with databases, like
3:13 SQLAlchemy now supports async and await.
3:15 There's things that work with
3:17 mongoDB, with the redis, and even files with async
3:21 and await. So you do have to have libraries that support them,
3:24 like httpx instead of requests,
3:27 but other than that, you're good to go.