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.
Two threads each run count++ once. The result can be 1, not 2.
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.
Each worker bumps its own copy of count. main's count never changes, so it prints 0.
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.
Two worker processes, each with its own GIL. Both the fetches and the CPU work run at the same time.
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