liveBook · Manning

Multiprocessing

As explained in Fast Python: High performance techniques for large datasets

Multiprocessing is a programming technique that executes work in multiple operating-system processes, enabling concurrent or parallel execution across available processor resources.

Multiprocessing runs work in separate processes rather than Python threads. The Global Interpreter Lock (GIL) restricts parallel execution of Python code in threads, but it does not restrict separate processes. Multiprocessing can therefore use multiple CPU cores for pure-Python work. section 1.3.1 section 3.6

A process-based MapReduce worker

For an in-memory MapReduce implementation, `concurrent.futures.ProcessPoolExecutor` provides a process-based alternative to `ThreadPoolExecutor`. The import can change to `from concurrent.futures import ProcessPoolExecutor as Executor`, but that change alone may leave the reduce stage apparently frozen. Inspecting completed futures reveals a pickling error when the worker function is a lambda. Defining it with `def counter(emitted): return emitted[0], sum(emitted[1])` resolves that specific failure. section 3.4 section 3.4.1

Processes communicate through pickle, so code and data sent to workers must be transferable by it or handled separately. File pointers, database connections, sockets, and some foreign-library objects need particular care. Serialization can also be slow enough to erase a parallel speedup. Favor coarse-grained jobs with little communication, and check whether communication—not computation—has become the bottleneck. Shared memory, low-level locks, and complex communication patterns make parallel code harder to debug. section 3.4.1 section 3.6

How Fast Python: High performance techniques for large datasets covers it

How deep it goes
Works through an executor substitution, diagnoses a pickling failure, and gives communication guidance without developing shared-memory or low-level locking techniques.
Examples
Uses a ProcessPoolExecutor MapReduce worker whose lambda fails to pickle and whose named-function replacement succeeds.
What it assumes
Builds on a threaded or asynchronous MapReduce implementation and familiarity with Python functions and futures.
What you can do after
Apply it via ProcessPoolExecutor to parallelize coarse-grained Python work and diagnose worker-transfer or communication bottlenecks.
How it connects
Connects multiprocessing to the GIL, multithreading, MapReduce, pickle serialization, and interprocess communication.

Also explained in

Compare how 3 books explain multiprocessing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fast Python: High performance techniques for large datasets. The section links point to the parts of the book it draws on.