Modern APIs with FastAPI and Python Transcripts
Chapter: Modern language foundations
Lecture: Concept: An async method
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
We saw how powerful asynchronous methods can be.
0:02
Let's just take a moment and review some of the core ideas around building async
0:06
methods. Normally, we define a method like "get HTML" as "def get_html"
0:11
but if you want it to be asynchronous,
0:13
you say "async def get_html" right.
0:15
We're gonna be doing that within FastAPI all over the place.
0:18
And then we have this somewhat unusual construct called an "async with block"
0:24
and that's probably the most unusual thing in async amd await.
0:26
And what we're going to do there is the actual with Block is opening a network
0:30
connection, opening a socket, and so we want to do that asynchronously.
0:34
It actually takes a little bit of time
0:35
if you just create these separately without the async stuff,
0:38
it's quite a bit slower. So this lets us open all the sockets at the
0:42
same time, and then we're gonna issue some of these requests.
0:45
We're gonna say "client.get". Now
0:47
normally, that's just a blocking call and you get a value back.
0:50
But when you call an asynchronous method,
0:52
which client.get is, what you get back is not the return value.
0:56
It didn't even actually run. What you get back is something called a co-routine
1:00
that if you await it, it will run and give you the value.
1:03
So here, we're going to say,
1:04
"await client.get", get the response,
1:06
and then we just work with it normal.
1:08
The big takeaway here is that where you see "async with", where you see
1:11
the word "await", all of those points,
1:14
if you're waiting on some external system, your program, in this case,
1:18
the web scraper, more generally, probably the API server, will be able to
1:23
actually handle other requests and do other work
1:26
until this client gets back to us with its answer,
1:28
then we pick it up and run with it.
1:31
Often, people think of this async await as scaling the computation or like adding
1:36
threads. Threads are not even really involved.
1:38
What it lets you do is it says it lets you mark different places in your
1:43
function that says "I'm waiting on something other than our program.
1:46
I'm waiting on something external, you can go to other work until that gets back
1:50
to us" right? So here we're waiting on the talkpython.fm server,
1:53
could be a database, it could be something else.
1:55
So it's this async with and await where we're marking,
1:58
like, "here you can go to other work until I get this response and then
2:02
please start me up and we'll keep running".
2:03
And that's how async and await works,
2:05
and because most Web applications are waiting on something else,
2:09
they're talking to databases, waiting on them,
2:11
they're talking to other API's, micro services,
2:13
and so on, waiting on them,
2:15
you can do a whole lot more work
2:17
if you can find a way for your Web server
2:18
to allow it to work on other stuff while it's waiting,
2:22
and that's exactly what async and await does for us.