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