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, again, you need an event loop, this manages your work. You need some co-routines.
0:07 You need to have functions that are suspendable, they have to have an await in there so that they can hand off the work to someone else,
0:13 they can't just be CPU heavy functions or they'll never hand off to someone else
0:17 and you'll basically be getting the same throughput that you would be getting by doing this in a synchronous manner. A couple of other things,
0:25 there's what's called a future and a future is something that may have a result in the future.
0:29 There's what's called a task, and a task is a subclass of a future that allows you to take a coroutine
0:34 and basically make it implement this future interface that Python uses. There's also what we call context switch
0:41 and basically context switch is when we call this await, under the covers this loop is going to switch from one of these co-routines to another one
0:48 and you can think of this, remember we talked about threading and that you can use native threads, we can think of this context switch
0:56 that rather than going from one thread to another we're going from a green thread to another green thread
1:00 a basic thread that's implemented inside of the virtual machine. That's a context switch, and this event loop manages that for us.
1:09 We talked a little bit about blocking and non-blocking. So blocking is you wait until your work is done before proceeding
1:15 we talked about that with our painting, you waited until your paint was completely dry before proceeding
1:20 and non-blocking, we hand off control while running, so if we're doing a non-blocking painting, we paint
1:26 and then if it needs to dry we go and paint something else, until that needs to dry and then we go pay something else and we repeat that process.