Compare

Process vs thread

A process is a running program with its own memory. Threads run inside a process and share that memory. Sharing is what makes threads cheap to communicate between, and also what lets them break each other’s data.

Threads share memory

Two Java threads run count++ on the same variable. Both read it before either writes, so one update is lost. That can only happen because they share count.

JavaWhy count++ loses updates

Two threads each run count++ once. The result can be 1, not 2.

0123456789main: t1.start()main: t2.start()join()main: print countT1: read 0T1: add → 1T1: write 1T2: read 0T2: add → 1T2: write 1
Play it step by step

Processes don’t

Four calls to bump() run in two Python worker processes. Each process has its own copy of count, so the main process still prints 0.

PythonProcesses don't share globals

Each worker bumps its own copy of count. main's count never changes, so it prints 0.

01234567main: Pool(2)pool.map(bump)main: print 0worker 1: start: own count = 0startworker 1: bump(): its own count += 1bumpworker 2: start: own count = 0startworker 2: bump(): its own count += 1bump
Play it step by step

The upside of separate processes

Each Python process has its own interpreter and its own GIL, so CPU-bound work runs in parallel. The cost is that data has to be sent between them, pickled and copied.

PythonThe same jobs with multiprocessing

Two worker processes, each with its own GIL. Both the fetches and the CPU work run at the same time.

0123456789main: Pool(2)pool.map()main: returnfetch: time.sleepcrunch (own GIL)donefetch: time.sleepcrunch (own GIL)done
Play it step by step

Common questions

What's the difference between a process and a thread?

A process has its own memory. Threads live inside a process and share its memory, so they can all see and change the same variables.

Do processes share global variables?

No. Each process has its own copy. In our Python test, four calls to bump() in a pool of two worker processes left the main process's count at 0.

Why use processes instead of threads?

Isolation, and in Python, parallelism: each process has its own GIL, so two worker processes used two CPUs at once in our tests, while two threads used one.

Every timeline here is checked against a real program in CI. How we verify