MongoDB with Async Python Transcripts
Chapter: Foundations: async
Lecture: Synchronous Execution Example
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
To truly grok the benefits that we get from async and await, especially around databases, we're going to imagine a web server.
0:11
And this web server can process requests for either an API call or for a web page request. It doesn't really matter. They're kind of the same.
0:20
Now, in this graphic, we have three requests. Requests 1, 2, and 3.
0:25
And these green boxes represent how much compute time is required to respond to that request.
0:31
So request one takes, you know, as block size, request two about the same, request three, really really quick.
0:38
But the person who makes request three, unfortunately, is in not a great order because two requests
0:43
have come in before, request one and two, which take a certain amount of time.
0:49
So from the perception of the caller from the perception of the person or the app making
0:55
the request, what is the total processing time or the response time as they see it in this system with this amount of load.
1:03
So in terms of response time, the first request comes in and it's exactly as long as the request
1:11
takes right that yellow bar same as the request length. Great, but request two has been waiting about 60, 70% of the time for request one to finish
1:21
before it could even get started. So its request is a lot slower. And poor old request three here. It should have been super quick,
1:29
but it's got to wait on both of these. And so it's perceived time is actually the longest by quite a bit, you know, five, 10 times, five times more
1:38
than the natural speed if it could have been processed in isolation. This is the scalability thing. As we add more work to the system,
1:47
it slows down more and more, okay? Not great. Now, if we zoom into one of these requests, let's say request one, doesn't matter.
1:56
It's not just a big block of code running. There are steps that are happening. There are systems and interactions that are at play.
2:03
So if we look at it, maybe the web framework, FastAPI is doing some work, and then it hands execution over to us,
2:09
and we decide, hey, we're gonna go make a query to the database, like, is a user logged in? If so, give us their user information back from their ID.
2:19
Our code checks and says, Hey, do you have access to do this thing or whatever with your account? Okay, great, you do.
2:26
Then we're gonna go back to the database with another call here and do a whole bunch of work. And then when that response comes back,
2:33
we do a little bit to prepare a response and return a JSON to the framework, the framework sends it back. Something like that, right?
2:41
Well, look at these places here. Because these are synchronous calls, there's no way to let other execution happen.
2:49
It's the call starts and it just runs our function. But what we're really doing is we're waiting. We're waiting not just on our code,
2:57
we're waiting on probably a separate computer who is running the database, maybe even the internet if it's some kind of cloud deal, right?
3:06
So we're waiting at minimum on another process. And we could be doing other work while this happens. But because we wrote it synchronously,
3:14
we call the function, it blocks until the whole thing is done. So there's no way to allow other processing to occur for the majority of our function,
3:24
which we're just waiting here. And that's the key insight that async and await and asyncio have is like, is there a way to break that series of steps
3:35
into smaller pieces? And when one of those pieces is waiting, it's not making progress, let's run other steps,
3:43
other slices of processing from other requests or other things happening in this program, right? So if we could make that red part become productive,
3:53
think how much faster our code could go. We got like five times performance improvement just because we can do things while we're waiting.
4:01
It's more mostly waiting.