Sub topic 10
Start methods: spawn, fork, forkserver, and the `__main__` guard
This chapter assumes you can use multiprocessing.Process or a process pool and know what pickling is. It explains how a child process gets created, why that affects your code, and why scripts that start processes need an if __name__ == "__main__": guard.
Three ways to create a child process
A start method decides how multiprocessing builds a new process. There are three.
| Method | How the child begins | Platforms | Cost |
|---|---|---|---|
| `spawn` | A fresh Python interpreter starts and imports your main module. | Windows, macOS, Linux | Slowest |
| `fork` | The OS copies the running parent, including its memory. | Unix only | Fastest |
| `forkserver` | A clean helper process is started once and forks children on request. | Unix only | In between |
The defaults differ by platform. Windows always uses spawn. macOS has used spawn since Python 3.8. Linux used fork through Python 3.13, and Python 3.14 changed the Linux default to forkserver. Don't rely on the default. Check it, or choose a method explicitly.
Why the guard is required
With spawn and forkserver, the child imports your main script to find the functions it must run. Without a guard, that import runs your process-starting code again, and each child would start more children. Python detects this and raises a RuntimeError about starting a process before bootstrapping has finished.
The guard stops the import from re-running that code:
import multiprocessing as mp
def square(x):
return x * x
if __name__ == "__main__":
print(mp.get_start_method())
ctx = mp.get_context("spawn")
with ctx.Pool(2) as pool:
print(pool.map(square, range(5)))When the child imports the script, __name__ is not "__main__", so only the definitions run. Everything that starts processes goes inside the guard. fork doesn't re-import the script, so code without a guard may seem to work on Linux and then fail on macOS or Windows.
Different methods, different state
fork copies the parent's memory as it is at that moment. spawn rebuilds the child from imports only. Module-level state changed at runtime therefore reaches a forked child but not a spawned one:
import multiprocessing as mp
config = {"mode": "default"}
def show(_):
return config["mode"]
if __name__ == "__main__":
config["mode"] = "changed"
for method in mp.get_all_start_methods():
if method == "forkserver":
continue
ctx = mp.get_context(method)
with ctx.Pool(1) as pool:
print(method, pool.map(show, [0]))On Linux this prints fork ['changed'] and spawn ['default']. The spawned child re-imported the script and never saw the assignment inside the guard. To get data to a child reliably, pass it as arguments, which are pickled, or send it through a queue.
Choosing a method
Use get_context(method) for a local choice. This avoids changing behavior for other libraries in the same program. mp.set_start_method(method) sets it globally, and it may be called only once unless you pass force=True. Call it inside the guard.
spawnis the safest and most portable. Pay the startup cost once by reusing a pool.forkis fast but unsafe if the parent already has threads. Locks held by other threads at the moment of the fork stay locked forever in the child. Python 3.12 and later warns when you fork a multi-threaded process.forkserveravoids copying a large, threaded parent. Children start from the clean helper process.
Exercise
A script starts a ProcessPoolExecutor at module level, with no __main__ guard. It works on your Linux machine with Python 3.12 but crashes with a RuntimeError on a colleague's Mac. Why, and what is the fix?
Show answer
Linux used fork by default there, so the children never re-imported the script. macOS uses spawn, so each child imports the script, reaches the pool-creating line, and tries to start more processes. Python raises the error. Move the process-starting code under if __name__ == "__main__":. You can also test on every platform by selecting spawn explicitly.