Python 3, an Illustrated Tour Transcripts
Chapter: Asynchronous Programming
Lecture: asyncio Building Blocks

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's look at some of the basics that are required to do this,
0:02 again, you need an event loop, this manages your work.
0:04 You need some co-routines.
0:06 You need to have functions that are suspendable,
0:08 they have to have an await in there
0:10 so that they can hand off the work to someone else,
0:12 they can't just be CPU heavy functions
0:14 or they'll never hand off to someone else
0:16 and you'll basically be getting the same throughput that you would be getting
0:20 by doing this in a synchronous manner.
0:22 A couple of other things,
0:24 there's what's called a future and a future is something
0:26 that may have a result in the future.
0:28 There's what's called a task, and a task is a subclass of a future
0:31 that allows you to take a coroutine
0:33 and basically make it implement this future interface that Python uses.
0:38 There's also what we call context switch
0:40 and basically context switch is when we call this await,
0:43 under the covers this loop is going to switch
0:45 from one of these co-routines to another one
0:47 and you can think of this, remember we talked about threading
0:51 and that you can use native threads,
0:53 we can think of this context switch
0:55 that rather than going from one thread to another
0:57 we're going from a green thread to another green thread
0:59 a basic thread that's implemented inside of the virtual machine.
1:03 That's a context switch, and this event loop manages that for us.
1:08 We talked a little bit about blocking and non-blocking.
1:10 So blocking is you wait until your work is done before proceeding
1:14 we talked about that with our painting,
1:16 you waited until your paint was completely dry before proceeding
1:19 and non-blocking, we hand off control while running,
1:23 so if we're doing a non-blocking painting, we paint
1:25 and then if it needs to dry we go and paint something else,
1:28 until that needs to dry and then we go pay something else
1:31 and we repeat that process.