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
0:02 that CPU graph that had a whole bunch of CPU cores
0:04 that were all black and just one of them was really busy?
0:07 What we're going to learn in this chapter
0:08 will let you max out your computer
0:10 and put all the CPU cores to work
0:13 assuming you have a ton of computation to do.
0:16 So, here's another Python program running
0:19 and this one happens to be doing way, way more work.
0:22 And we're going to use the multiprocessing technique
0:24 which is the same thing I used to generate this picture.
0:28 So, what's the fundamental idea here? Remember the GIL.
0:30 The GIL was the thing that actually prevented
0:33 our Python code from scaling across multiple threads.
0:36 Sure, if that Python code is calling something down
0:39 to the network layer or is calling something over
0:41 to a database it'll release the GIL
0:44 and while it's waiting we can do other stuff.
0:46 But computationally, one Python interpreter instruction
0:50 gets to run at a time, that's it.
0:52 And the GIL is blocking those.
0:54 So, here you can one process it's running.
0:57 It has actually three threads
0:58 but only one of them is allowed to go
1:00 'cause the GIL is blocking the other two.
1:02 And, of course, this cycles around and it switches.
1:04 How do we get that cool picture worth all the green
1:07 all the work being done?
1:08 Well, we don't let the GIL see the work.
1:09 In fact, we're just going to kick off a bunch of processes.
1:12 And, yes, these are Python processes
1:13 and they each have their own GIL
1:15 but they don't interfere with each other
1:17 just like one program doesn't interfere with another.
1:20 And in that regard, we can basically ignore Python's GIL
1:23 because every single bit of work we're going to spawn off
1:27 from what would have been a different thread
1:28 is now going to be in a dedicated process.
1:30 So, this can all happen in parallel.
1:32 And you'll see the multiprocessing library is really great
1:35 at making this fairly transparent
1:37 and especially exchanging the data so
1:39 that we can call the process
1:41 wait on it and get the data back
1:42 as if we just almost called a function, it's great.
1:46 This is the picture to keep in mind for this chapter
1:49 and we're going to go write code that does this next.