Python 3, an illustrated tour Transcripts
Chapter: Asynchronous Programming
Lecture: asyncio When and Why

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So Python provides in 3.6 these new keywords, async and await,
0:05 and if you put async in front of a def that makes it a coroutine
0:10 that you can use in this asyncio framework,
0:13 and when you want to hand off control of something else you call await.
0:17 So the benefit of this asyncio library is that
0:21 it allows you to write asynchronous code in a sequential style.
0:24 Remember I had that slide where I said
0:26 if you get rid of the async and the await, it looks sequential.
0:29 There's other asynchronous libraries in Python
0:31 and many of them allow you to write asynchronous code
0:34 but do so with callbacks and if you're familiar with callbacks that can get a little confusing,
0:39 and Guido van Rossum, the creator of Python
0:41 who actually worked on asyncio for a long time, is not a big fan of callbacks,
0:46 and so he didn't really want to introduce callbacks into the standard library,
0:50 but he wanted something that allowed us to take advantage
0:53 of asynchronous programming, but make it look as normal
0:57 and someone who is used to Python programming
1:01 can look at it and should be able
1:03 to wrap their head around what's going on there.
1:06 Why or when would you want to take advantage of this?
1:08 Again, if you have lots of io,
1:11 these asynchronous programs scale better than threads or processes.
1:17 There is overhead to creating a thread in Python,
1:20 Python threads are native threads, they are operating system threads
1:24 and there is overhead the order of megabytes per thread.
1:27 And so if you've got thousands of requests coming in to a web server or whatnot,
1:32 you've got each of those as a thread,
1:35 each of those is going to have some overhead to it
1:38 and a process has even more overhead than a thread does.
1:42 But if you have stuff that has a lot of io latency such as a server or whatnot,
1:47 then you can use these native threads or this asynchronous style programming
1:51 to scale better, but one thing to be aware of is that your whole stack
1:56 basically needs to be asyncio aware.
1:58 Once you put code in there that blocks or just takes up the CPU
2:02 and doesn't allow anything else to run
2:05 you're going to have throughput that's going to suffer
2:07 because it's not going to allow other code to run.
2:10 So you really need to have code that has awaits in it
2:14 and allows other code to run.
2:17 So some of the components that you all need,
2:19 we need a function that will suspend and resume these co-routines.
2:21 We need an event loop that keeps track of the functions and their states
2:24 and we'll run them and manage them.
2:26 If we have long-running CPU tasks, these need to call await.
2:31 So the other code can run or else we need to use
2:34 what's called an executor to run those in their own thread
2:37 so that they don't interrupt or basically block
2:41 the asynchronous code from running.