Python for the .NET Developer Transcripts
Chapter: async and await in Python
Lecture: C#'s async version

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

Talk Python's Mastodon Michael Kennedy's Mastodon