Async Techniques and Examples in Python Transcripts
Chapter: Leveraging CPU cores with multiprocessing
Lecture: Introduction to scaling CPU-bound operations

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Remember at the beginning when I showed you that CPU graph that had a whole bunch of CPU cores
0:05 that were all black and just one of them was really busy? What we're going to learn in this chapter will let you max out your computer
0:11 and put all the CPU cores to work assuming you have a ton of computation to do. So, here's another Python program running
0:20 and this one happens to be doing way, way more work. And we're going to use the multiprocessing technique
0:25 which is the same thing I used to generate this picture. So, what's the fundamental idea here? Remember the GIL.
0:31 The GIL was the thing that actually prevented our Python code from scaling across multiple threads. Sure, if that Python code is calling something down
0:40 to the network layer or is calling something over to a database it'll release the GIL and while it's waiting we can do other stuff.
0:47 But computationally, one Python interpreter instruction gets to run at a time, that's it. And the GIL is blocking those.
0:55 So, here you can one process it's running. It has actually three threads but only one of them is allowed to go
1:01 'cause the GIL is blocking the other two. And, of course, this cycles around and it switches. How do we get that cool picture worth all the green
1:08 all the work being done? Well, we don't let the GIL see the work. In fact, we're just going to kick off a bunch of processes.
1:13 And, yes, these are Python processes and they each have their own GIL but they don't interfere with each other
1:18 just like one program doesn't interfere with another. And in that regard, we can basically ignore Python's GIL
1:24 because every single bit of work we're going to spawn off from what would have been a different thread is now going to be in a dedicated process.
1:31 So, this can all happen in parallel. And you'll see the multiprocessing library is really great at making this fairly transparent
1:38 and especially exchanging the data so that we can call the process wait on it and get the data back as if we just almost called a function, it's great.
1:47 This is the picture to keep in mind for this chapter and we're going to go write code that does this next.


Talk Python's Mastodon Michael Kennedy's Mastodon