MongoDB with Async Python Transcripts
Chapter: Foundations: async
Lecture: Asynchronous Execution Example
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
What if we were to use asynchronous execution? In particular, what if we could run other code and process other requests while our
0:11
original one is waiting? That's the big red waiting on database section from request one. Well, how would this look instead?
0:20
So here we have a request one come in and we start processing it.
0:24
We make a little bit of blocking execution until we get to that database part and then And then we just go, all right, we're waiting.
0:32
Anybody else want to run? Right around then request two comes in and it starts to process and it gets to its database part and says,
0:39
well, I'm waiting on the database. One and two are now both waiting. So there's hanging out and the system says,
0:46
anyone else want to run while these things are waiting? Sure, request three can come in. Maybe it's a little bit of a delay
0:51
for that to get processed, to get started, but then it's processing starts. How does this look in terms of response time to the users,
0:58
the consumers of this API, way better. Well, request one is gonna take just like before the same amount of time it takes. But importantly, request two,
1:09
or response two comes back in just about the time that it would take if it were in isolation. And because request three was able to run
1:17
completely during one of these were both waiting on the database sections, response three is also pretty much as fast as it would be in isolation.
1:26
Here's that scalability thing I was talking about. If we have one request or we have three requests, the perceived response time is about unchanged.
1:36
Pretty awesome. Again, if we zoom in, same thing. We've got our framework database, our code, more database, more code, more framework.
1:44
But this part, we are now able to say, Hey, Python, right now we're waiting on an external system, some form of IO, network IOs in particular.
1:55
If you got other stuff to do, go do it. And when that IO finishes, wake us back up and run our code.
2:00
Do that a couple times, and that creates productive waiting instead of unproductive waiting. Instead of saying, sorry, we're blocked,
2:07
waiting on the database, can't do anything. And this is the key step that allows Python to do way more work with asyncio
2:17
if we just use the await, the async and await keywords.