Python for the .NET developer Transcripts
Chapter: async and await in Python
Lecture: Async for scalability
0:00 Let's start with scalability.
0:03 Suppose we're running a web app, a web server
0:06 and we want to do as much processing
0:08 with the minimal amount of resources as possible.
0:11 One option would be to spin up a whole bunch of threads
0:15 and have each thread handle a request.
0:18 That would work but that's actually a lot of overhead.
0:21 Threads take up a lot of memory
0:22 the contact switching can be high.
0:25 If we do them in order
0:26 and request one then request two and request three
0:28 like in this example here.
0:30 Well, we'd have to stack them on top of each other
0:32 and that would be super slow.
0:33 But as I laid out in the beginning
0:35 these requests are generally for web apps
0:38 mostly about waiting.
0:39 I got a little bit of stuff from the requests
0:41 and then, oh I need to take this user id in the cookie
0:44 and go ask the database for the user details
0:47 or once I get the user details
0:48 I'm going to call an external API like, you know
0:51 charge a credit card or something
0:52 and I wait on that. And then I get it back
0:55 and then I'm going to say, Oh here's the, you know
0:56 update their record, yeah, now they own this thing
0:59 in the database and wait again on that.
1:01 And then I'm going to return some stuff over the network.
1:03 So all of this waiting means that
1:05 we can break up our requests
1:06 and while though we're waiting
1:07 we can just immediately pick up the other thing.
1:09 So if we look at the response time
1:11 yeah, we're going to take however long
1:13 those waiting periods take for response one to execute
1:16 but we don't have to wait
1:18 for response one to be totally done.
1:19 There's probably right at the top of two is
1:22 we're probably already just waiting
1:23 on some database or something.
1:24 So we can go ahead and just put that work aside
1:27 using the async and await keyword
1:28 pick up request two and get it going down the road
1:31 until it starts waiting on the database for something.
1:34 That pretty much means the latency of the additional time
1:36 for two requests versus one at a time is really, really low.
1:40 Again, request three here
1:42 we can respond pretty much right away.
1:44 With just a little bit of AsyncIO
1:46 a little async and await, we can do a lot of magic
1:49 to speed up this execution
1:52 and we can do this even with just a single thread
1:54 without adding all the overhead
1:56 of many, many, many threads.
1:58 This is the fundamental idea of how Node.js
2:00 is actually able to handle a ton of concurrent requests
2:03 and you'll see that's also what Python's idea is.