Python for .NET Developers Transcripts
Chapter: Course conclusion
Lecture: async and await review

Login or purchase this course to watch this video and the rest of the course contents.
0:00 One of the most amazing features of C# is it's async and await framework in the task parallel library. And guess what? Python has it too!
0:09 Over here we're going to create a GET HTML function and it's going to go over to my podcast grab a episode number, use that as the URL
0:18 and then download that page use a little bit of screen scraping after this to figure out what the title is. That's the demo that we did, anyway.
0:26 So we want this method to be able to run alongside other methods, right? Really, all you're doing is waiting
0:31 on the internet, waiting on the Python Server. Why should your code go slow and do that one at a time? You can send a bunch of those off.
0:38 So, here we're going to asynchronously define a method get_HTML, and then we can use async and await in the context.
0:46 In C# you just have await in the body of the method but here we have async context managers.
0:52 Alright, so we have async with httpx.AasyncClient() as client and the async part there make sure that we asynchronously close
0:59 any pending network connections with a keep alive or something like that. Then we're going to call client.get.
1:06 That returns to coroutine that when we await it is going to return a web response and then we're just going to work with that from there.
1:12 That's the way we build async methods in Python. Remember you have to manually manage the loop and all of that stuff, it's a little bit cumbersome
1:21 so consider using the unsync library to even more like C#s task per the library.


Talk Python's Mastodon Michael Kennedy's Mastodon