Python for the .NET developer Transcripts
Chapter: async and await in Python
Lecture: Anatomy of an async method

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's quickly review creating an asynchronous method
0:03 in Python, that actually does asynchronous work.
0:06 Here's the get_html method given an episode number.
0:09 It's going to go construct a URL
0:11 and then asynchronously call out to
0:15 figure out the content of that page
0:17 make sure everything worked, and then return the text.
0:20 So the first requirement, just like C#
0:22 is that the method is designated as asynchronous.
0:25 So we have async def get HTML.
0:27 Once that's in there we can then start using
0:30 the async and await keywords inside the method.
0:33 So Python has this surprising syntax
0:36 about asynchronous context managers
0:38 but, of course, there's network stuff going on in there
0:40 you would probably want that to be a asynchronous
0:43 this is how we tell the run time to do that.
0:46 So we say async with
0:48 right, otherwise that's a standard with statement.
0:50 And then the main thing that we're going to do
0:52 is we're going to go to the client and we're going to call get.
0:54 That's where we're waiting for the response from the server
0:56 so we use the await keyword.
0:59 You saw without the await keyword we get a coroutine
1:02 who's return type when finished is this response.
1:05 But if we await it
1:06 it gets converted into the response directly right here.
1:10 This is exactly like the await keyword in C#.
1:13 Normally we would get a task of a response
1:15 a generic task or response back.
1:17 But if you await it, you just get a response.
1:19 So your intuition from async and await in C#
1:22 is very, very applicable over here.