From the course: Complete Guide to Parallel and Concurrent Programming in Python

Unlock the full course today

Join today to access over 24,900 courses taught by industry experts.

Multiple processes: Python demo

Multiple processes: Python demo

- [Instructor] To leverage multiple processors and achieve true parallel execution in Python, rather than structuring our program to use multiple threads, we'll need to use multiple processes. Fortunately, Python's multiprocessing package makes that pretty straightforward because it provides an API for spawning additional processes that looks very similar to the threading module. To demonstrate just how similar they are, I'll modify the code from the previous example, which created several threads running the CPU waster function to use several processes instead. First, I'll need to import the multiprocessing module. And since that's a long word, I'll rename it to MP for short. Next, on Line 21, I'll replace the threading module's Thread class with the multiprocessing package's Process class. And finally, when using the multiprocessing package to spawn additional processes, it's important to enclose the main body of the program in an IF statement. Then check if the special name…

Contents