Python for .NET Developers 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 in Python, that actually does asynchronous work.
0:07
Here's the get_HTML method given an episode number. It's going to go construct a URL and then asynchronously call out to talkpython.fm
0:16
figure out the content of that page make sure everything worked, and then return the text. So the first requirement, just like C#
0:23
is that the method is designated as asynchronous. So we have async def get HTML. Once that's in there we can then start using
0:31
the async and await keywords inside the method. So Python has this surprising syntax about asynchronous context managers
0:39
but, of course, there's network stuff going on in there you would probably want that to be a asynchronous this is how we tell the run time to do that.
0:47
So we say async with right, otherwise that's a standard with statement. And then the main thing that we're going to do
0:53
is we're going to go to the client and we're going to call get. That's where we're waiting for the response from the server
0:57
so we use the await keyword. You saw without the await keyword we get a coroutine who's return type when finished is this response. But if we await it
1:07
it gets converted into the response directly right here. This is exactly like the await keyword in C#. Normally we would get a task of a response
1:16
a generic task or response back. But if you await it, you just get a response. So your intuition from async and await in C#
1:23
is very, very applicable over here.