PYTHON ADVANCED: PROFESSIONAL ENGINEERING MASTERY / L07MULTIPROCESSING: TRUE PARALLELISM IN PYTHON
课程 · 12 · 07 / 12
LESSON 07 · ADVANCED · 55 MIN · ◆ 2 INSTRUMENTS

Multiprocessing: True Parallelism in Python

Bypass the GIL with multiprocessing. Use process pools, manage shared state safely, and understand inter-process communication.

TIP

Learning Objectives: After this lesson, you'll understand how to bypass the GIL with multiprocessing, use process pools efficiently, manage shared state safely, and implement inter-process communication patterns.

Why Multiprocessing?

Multiprocessing creates separate Python processes, each with its own GIL, enabling true parallelism.

FIG. 02Flow Diagram
INTERACTIVE
LOADING INSTRUMENT
Fig. 02Interactive flow diagrams, timelines, and process visualizations

Multiprocessing vs Threading

FIG. 04Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 04Interactive Python code execution environment

Basic Process Creation

FIG. 06Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 06Interactive Python code execution environment

Process Pools

Process pools manage a fixed number of worker processes for efficient task distribution.

FIG. 08Flow Diagram
INTERACTIVE
LOADING INSTRUMENT
Fig. 08Interactive flow diagrams, timelines, and process visualizations

Using Pool

FIG. 10Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 10Interactive Python code execution environment

Pool Methods

FIG. 12Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 12Interactive Python code execution environment

Shared State and Communication

Processes don't share memory by default. Here's how to share data.

FIG. 14Flow Diagram
INTERACTIVE
LOADING INSTRUMENT
Fig. 14Interactive flow diagrams, timelines, and process visualizations

Shared Memory Values

FIG. 16Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 16Interactive Python code execution environment

Manager for Complex Shared Objects

FIG. 18Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 18Interactive Python code execution environment

Queues for Communication

FIG. 20Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 20Interactive Python code execution environment

Pipes for Two-Way Communication

FIG. 22Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 22Interactive Python code execution environment

Practical Patterns

Map-Reduce Pattern

FIG. 24Flow Diagram
INTERACTIVE
LOADING INSTRUMENT
Fig. 24Interactive flow diagrams, timelines, and process visualizations
FIG. 26Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 26Interactive Python code execution environment

Parallel Pipeline

FIG. 28Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 28Interactive Python code execution environment

Worker Pool with Callbacks

FIG. 30Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 30Interactive Python code execution environment

Best Practices

FIG. 32Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 32Interactive Python code execution environment

Key Takeaways

ConceptDescription
ProcessSeparate Python interpreter, own GIL
PoolReusable worker processes
ProcessPoolExecutorModern, clean pool interface
Value/ArrayShared memory primitives
ManagerShared complex objects (dict, list)
QueueThread/process-safe communication
PipeTwo-way process communication

Multiprocessing vs Alternatives

ScenarioBest Choice
CPU-heavy calculationMultiprocessing
Network I/OThreading or asyncio
File I/OThreading
Many small tasksPool with chunksize
Large shared dataSharedMemory (Python 3.8+)
NumPy operationsNumPy (already parallel)

Next Steps

In the next lesson, we'll explore Async/Await Fundamentals—understand event loops, write coroutines, use asyncio for concurrent I/O, and build async patterns for production code.


Ready for non-blocking Python? Async/await awaits!


Further Reading

Official Docs

Tutorials

Modern Parallel Python

  • joblib — the parallelism library used by scikit-learn. Cleaner API than raw multiprocessing for embarrassingly-parallel work.
  • ray — distributed Python. Scales from one machine to a cluster with @ray.remote.
  • dask — parallel computing for analytics; familiar pandas/numpy APIs.
  • mpire — modern multiprocessing wrapper with progress bars and worker reuse.

Common Pitfalls

Books

  • Book: Fluent Python (2nd ed.) — Chapter 20 ("Concurrent Executors").
  • Book: High Performance Python — Gorelick & Ozsvald (2nd ed., 2020). Covers when multiprocessing actually helps vs. just adding overhead.