From the course: Python Parallel and Concurrent Programming Part 1

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Multiple processes: Python demo

Multiple processes: Python demo - Python Tutorial

From the course: Python Parallel and Concurrent Programming Part 1

Start my 1-month free trial

Multiple processes: Python demo

- To leverage multiple processors, and to 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 straight forward, 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 multi`processing 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 modules thread class with the multiprocessing packages process class, and finally, when using the multiprocessing package to spawn additional processes, it's important to enclose the main body of the program,…

Contents