Async Techniques and Examples in Python Transcripts
Chapter: async and await with asyncio
Lecture: Concept: asyncio
0:00 Now that you've seen our asyncio example in action
0:03 let's look at the anatomy of an async method.
0:05 Remember there's two core things we have to do.
0:08 We begin by making the method async.
0:11 So we have a regular method def method name and so on.
0:13 To make it async you just put async in front.
0:17 Remember requires Python 3.5 or higher.
0:20 And then you're going to await
0:21 all of the other async calls that you make
0:24 so anytime within this method
0:26 if you're going to call it another async method
0:28 you have await it.
0:30 And that tells Python here is a part, a slice of this job
0:34 and you can partition with other slices of other jobs
0:37 in the asyncio event loop.
0:40 So we know that get is an async method.
0:43 Python didn't help us this time very much on this one
0:45 but if you go to the definition you'll see
0:48 and also we didn't actually get a tuple back
0:49 we got a coroutine, so that's a dead giveaway there.
0:52 And that's pretty much it.
0:53 You don't really have to do much differently at all.
0:56 The most important thing is that anytime you can
0:59 that you use some sort of method
1:01 that actually supports asynchronous calls.
1:04 So if you're talking to a database try to find the driver
1:07 that supports asynchronous operations and so on.
1:10 We're going to talk more about that later
1:11 as we get farther in the class
1:12 but the more times you can await stuff
1:14 the more fine-grain the work will be
1:16 probably the better.