Python 3, an Illustrated Tour Transcripts
Chapter: Asynchronous Programming
Lecture: Parallelism and Asynchrony Compared

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So here's an example of parallelism. We talked about concurrency here, we have three Python processes
0:06 and each of them is going to create their own painting. And so the first one can do a little bit of work and do a little more work
0:12 and if I had multiple cores, if I had at least three cores while that one was running this other process could be doing his work
0:19 and another process could be doing its work, and so we can see that this takes about a third as much time
0:25 as our prior concurrent version, if we are able to do this in a parallel manner.
0:30 Now, let's go back to our concurrent way when we have one Python process and let's think about maybe painting a little bit more
0:38 if you've painted a little bit, you might know that when you're painting you put some paint on the canvas
0:43 or if you're water coloring you get the paper wet and then you paint on it
0:48 and then typically you have some period after you've painted where you let it dry. And so we're going to just put on some grey blocks here
0:55 and this grey block indicates that the paint is drying, now, if you are painting three paintings,
1:01 it could be the case that you paint and then dry and wait for that drying and then start working on the next one.
1:08 And this would be a synchronous manner of doing that but you'll note that all this drying time here with the gray drying time,
1:14 you're not really painting, you're just waiting for it to dry. So that could be wasted time.
1:18 If we move this to an asynchronous model and the asynchronous model says if I'm going to be waiting on something
1:25 and I know that at some point it will be done, as soon as I need to start waiting, like I start painting and then I start waiting here,
1:32 I'm going to say go ahead and wait, but I'm not going to wait till you're done drying,
1:37 I'm just going to go off and I'm going to start painting on my next guy until he needs to dry, and then I'm going to start painting on my next guy
1:44 and I'm going to repeat that. And that way I can take advantage of this drying time or whatnot. This grey indicates drying time.
1:54 This is similar to code in the real world. There is some code where you do some CPU heavy work,
1:59 but then you have some work that's what we call IO bound where it's going over the network or it's going over the file system
2:06 and Python is not really doing anything, but it's waiting for data to come back. So this is what we call IO bound
2:13 and if you have something that's IO bound and you have lots of IO bound stuff, then you can take advantage of this asynchronous way of programming
2:22 to not worry about IO bound stuff and move on to other stuff.


Talk Python's Mastodon Michael Kennedy's Mastodon