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

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We're going to look at asyncio, specifically pep 526 and the tooling that came in Python 3.6. Asyncio came about during Python 3.4 time frame
0:13 and Python 3.6 added some new syntax to make it nicer to use. Let's talk about some terms first to get these straight
0:20 what is concurrency, concurrency means that we're sharing resources. One way to visualize this is a juggler who has multiple balls.
0:29 Each of those balls is a resource and he's juggling them. Similarly, a CPU on a computer juggles multiple resources. It can run multiple things,
0:39 a single CPU cannot run multiple things at the same time, but what it's doing is it's shifting between those very quickly.
0:44 Parallelism, which some people confuse for concurrency, but it's different, parallelism means doing multiple things at the same time.
0:51 So if I have some parallel code, it can run something faster than doing something at once. So an example here would be a CPU that has multiple cores.
1:04 It can run code twice as fast as that code is run to take advantage of both CPUs. If we're going back to our juggler example,
1:12 this would be multiple jugglers juggling multiple balls, presumably multiple jugglers who are juggling can juggle more balls
1:19 than just one juggler, if they don't have to coordinate among themselves per se. A couple of other things to be aware of,
1:25 one is a thread, a thread is a operating system construct for doing something, a thread runs on a CPU, if you have multiple CPUs
1:34 and your code can run in multiple threads, it's possible that each of those threads could take advantage of a single CPU and make it run faster.
1:43 Now, this isn't the case in Python, Python has what's called the gil or global interpreter lock
1:48 which limits multi-threaded code in Python to only run on one CPU regardless of how many CPUs are on the system.
1:55 Another thing to be aware of is what's called a green thread, green threads are VM level threads, so they're not done at the operating system level
2:03 but they're done at a programming or user level and these are a little bit lighter weight, but they don't scale across CPUs.
2:10 And we'll see how asyncio basically allows you to use green threads or run different contexts across a CPU inside of a VM,
2:22 but doesn't necessarily allow you to take advantage of multiple CPUs. A couple other terms we'll talk about here,
2:29 synchronous, synchronous means if I'm going to run something, if I'm synchronous I wait till the execution is done before I run something else.
2:36 And asynchronous code is where I kick off execution and maybe I have a callback or some way to figure out when it's done
2:42 and after I kicked off execution, I'm going to move on to some other code until I get this call back or mechanism that tells me that it's done.
2:50 That's asynchronous code.

Talk Python's Mastodon Michael Kennedy's Mastodon