Log inStart free
LESSON 1.10 · 6 STAGES · 4 CONTROLS · 3 CHECKSPRO · PREVIEW

Multiprocessing

Watch a pool split 8 tasks across processes that share no memory.


Pro lesson. Stage 1 is free to watch. Pro opens all 6 stages, 4 controls and 3 checks, in this lesson and every other.

Go Pro · $8.25/mo
EXPLAINER · 1.10 · PROCESS BAY3 PROCESSES · 2 WORKERS · FORK · 10 MS
01
Stage 1 of 6 · Start workers
Pool(2) starts 2 worker processes in 10 ms. fork gives each one a copy of the parent's memory.
Explain it simply

A process is a running program with its own memory. Pool(2) starts 2 worker processes beside the parent. With fork, each worker starts as a copy of the parent's memory: count, task, TASKS and data. After that, the copies are separate, so a change in one never reaches another.

The parent process, pid 4110, holds count, task, TASKS, data and pool. 2 worker processes start in 10 ms. Each worker holds count, task, TASKS, data.

process memorytask data, Python workI/O waitworker start, data copyresultpickling error
parent processpipesworker processesprocesses = 1 + 2 = 3parentpid 4110with Pool(2) as pool:count0taskƒ taskTASKSiiiiccccdata7poolPool(2)task pipe →← result pipeP1pid 4111fork: a copycount0taskƒ taskTASKSno taskdata7P2pid 4112fork: a copycount0taskƒ taskTASKSno taskdata7I/O 1P1I/O 2P2I/O 3I/O 4CPU 1CPU 2CPU 3CPU 40100200300400ms10 msparent processpipesworker processesprocesses = 1 + 2 = 3parentpid 4110with Pool(2) as pool:count0taskƒ taskTASKSiiiiccccdata7poolPool(2)task pipe →← result pipeP1pid 4111fork: a copycount0taskƒ taskTASKSno taskdata7P2pid 4112fork: a copycount0taskƒ taskTASKSno taskdata7I/O 1P1I/O 2P2I/O 3I/O 4CPU 1CPU 2CPU 3CPU 40100200300400ms10 ms
Toy: 1.C1's 8 tasks · 10 ms worker start · copies at 100 MB/s · real pickle bytes (protocol 4) · pids are stand-ins
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
processes=1+w\text{processes} = 1 + \sy{w}{param}{w}
3=1+2,each starts in 10 ms3 = 1 + \sy{w}{param}{2},\quad \text{each starts in } 10\ \text{ms}

The pool adds w worker processes, each with its own memory.

Common mix-ups

MYTHA worker's count += 1 changes the parent's count.

The fix, and 1 more mix-up with the diagram to show them, come with Pro.

Predict, then reveal
3 CHECKS · PRO
Q01With 4 workers instead of 2, does the pool take half the time?

Make your guess, then test it in the diagram. All 3 checks come with Pro.

PRO LESSON · YOU WATCHED STAGE 1 OF 6
Unlock the full explainer
  • 5 more stages and 4 live controls
  • 2 mix-up cards and 3 predict-then-reveal checks
  • Watch it as a narrated video, in three voices
  • Every Pro lesson and capstone · 656 in total

$99 a year, or $15 a month. Cancel any time.