Multiprocessing Idioms and Patterns in Python

Programming in Python is fun, you can get maximum done with minimum means…for the most part. Lately, I’ve been playing around with Python’s multiprocessing module, I thought I’d use some of the long weekend time to write up this post summarizing some general patterns (potentially) for others to leverage.

Multithreading in Python can be smoke and mirrors coz of the Global Interpreter Lock(only one thread executes Python bytecode at any given point of time), however, since multiprocessing module lets you spawn multiple python processes that can run on multiple CPU cores simultaneously and provides with some good built in synchronization techniques, you can do some real parallel programming here.

The Pool:

The following piece of code- uses multiprocessing's Pool to create a pool of worker processes that take a method and the input data, prallelizes the execution, returns the output (sorta like MapReduce). It also shows how you can pass multiple argumens to a pooled method (which textbook examples don't usually cover) and it has some easter eggs if you can find them 😉

Things to watch out for while using Pool:

  1. You can't have multiple layers of pool as in- a pool of processes using another pool of processes underneath. This is the biggest 😔 for me with pool
  2. Watch out for zombies- make sure your pool is properly terminated at the end
  3. Higher pool sizes can be counter productive; strike the right balance- Keep context switching in mind

In a future post, I'll discuss multiprocessing's Process module and techniques related to- sharing state across multiple processes and synchronization.