Python for the .NET developer Transcripts
Chapter: async and await in Python
Lecture: C#'s async version
0:00 As is our custom for this course
0:02 we are going to first look at what we're trying
0:05 to build in C#, and then
0:07 we're going to go build it in Python.
0:09 Now, what we're actually looking at here is not new.
0:12 Remember, over here in NuGet
0:14 we wrote this program that would go
0:16 and it would do this webscaping stuff
0:19 and it would go download ten different episodes of pages
0:23 of the pages for the episodes
0:25 of my podcast, Episodes 220 to 230
0:28 and then, it would use the HTMLAgilityPack
0:31 to pull out the HTML.
0:33 Over here, where we have GetEpisodeHtml
0:36 this line 48, actually, sorry.
0:40 This line right there is a blocking called
0:42 actually, I kind of faked it
0:44 I don't know if you look carefully there, get async.result.
0:47 So I said, Hey, hey, I don't actually want to do async
0:50 and await stuff, I just want to make this run
0:52 synchronously so we can compare it to what we have
0:55 directly over at Python.
0:57 I don't think the HTTP client actually has an option
1:00 for a synchronous version, but, you know
1:02 this is effectively a synchronous version.
1:04 Over here, in the Chapter 9 async
1:07 we've unleashed the async beast here, if you will.
1:10 Over here, where we do GetEpisodeHtml
1:15 we're actually a way in.
1:16 So, we have to do a little bit of juggling here.
1:18 What we would like to do is just go
1:20 for all these ten episodes, just call GetEpisodeHtml
1:24 and the most natural way would be to, like, loop through it
1:27 and then just, for each one call this
1:29 and await this directly.
1:32 But what happens is the program would be more scalable
1:35 it potentially could do other stuff, but it would not make
1:38 this individual task any faster
1:41 because we would still be waiting for one
1:42 and then the next and the next.
1:44 So in order to get true parallelism
1:46 we're creating a list of tuples. Woo!
1:50 Each tuple contains an integer, the episode number
1:52 and a Task<string> which is the pending download task for this.
1:58 So what we do is we go through there
1:59 and we start the task right there
2:01 and then we put it into this list
2:03 so at this line, once that's all done
2:05 all the downloads have started, and then we go
2:08 for each one of them, we're going to await the task
2:11 and get the HTML.
2:12 So slightly more complicated
2:14 but that's needed to get the true parallelism out.
2:17 Now, let's go ahead and run this.
2:20 So, notice, it gets all, starts getting all of them
2:22 and then it gets all the responses
2:23 and it takes only 1.7 seconds.
2:25 I don't know if you remember
2:26 but the other one took almost 10 seconds.
2:29 Let's do it one more time.
2:34 A little bit longer, 1.9. Let's do it a third time
2:36 just so we have the timing pretty reliable here.
2:38 1.7, 1.9, 1.6, let's just say it's 1.75
2:43 Or something like that on average, right?
2:45 This 1.6 is the best we've been able to get.
2:47 The website responds super fast
2:49 but it's actually on the other side of the United States
2:51 the absolute other side, the East Coast, in New York.
2:54 So that means it's going to take, you know, the ping time
2:57 from here to New York, just on
2:59 on, each direction, right, to get this.
3:01 So, it's, there's a built-in delay from the network
3:03 but it's kicking off, you can see the thread pool
3:06 is running different threads
3:07 and all sorts of good stuff down here.
3:09 Alright, this C# version, this adaptation
3:12 of the static one, the serial one, the sequential one
3:17 over to this async version
3:19 that's what we're going to do in Python as well
3:21 we're going to see how to do this over in Python
3:23 and it turns out, it's remarkably similar.
3:26 Pretty cool, actually.