Featurecore
The multiprocessing module
Also called multiprocessing, Process, Pool, start method, fork, spawn, forkserver, process-based parallelism, Manager, shared memory.
The multiprocessing module runs Python code in other processes. Each process has its own interpreter and its own GIL, so CPU-bound work runs in parallel. Arguments and results cross by pickling.
- Language
- Python
Synopsis
from multiprocessing import Pool
def square(x):
return x * x
if __name__ == "__main__":
with Pool(2) as pool:
print(pool.map(square, range(6)))
[0, 1, 4, 9, 16, 25]
Semantics
r1. The module "supports spawning processes using an API similar to the threading module". It sidesteps the GIL "by using subprocesses instead of threads", so it can "fully leverage multiple processors on a given machine."1
r2. "Process objects represent activity that is run in a separate process. The Process class has equivalents of all the methods of threading.Thread." A program calls start() and then join().2 Example: process-exitcode.
r3. There are three start methods. With spawn, "The parent process starts a fresh Python interpreter process." With fork, the child "is effectively identical to the parent process", but "safely forking a multithreaded process is problematic." With forkserver, a server process forks each new process.1 Example: start-method.
r4. "The global start method can only be set once." To use another method in one part of a program, get_context() returns a context object that has "the same API as the multiprocessing module".12
r5. Processes do not share memory by default. A child sees its own copy of each global: with spawn and forkserver, "the value it sees (if any) may not be the same as the value in the parent process at the time that Process.start was called."3 Example: separate-memory.
r6. Objects cross between processes by pickling. "The arguments to Process usually need to be picklable", and "Any object put into a multiprocessing queue will be serialized."1 Example: queue-result.
r7. For shared state, Value and Array store data "in a shared memory map", and "These shared objects will be process and thread-safe." A Manager() "controls a server process which holds Python objects and allows other processes to manipulate them using proxies."1 Example: shared-value.
r8. Pool.map is "A parallel equivalent of the map() built-in function". It "chops the iterable into a number of chunks which it submits to the process pool as separate tasks."2
r9. With spawn or forkserver, the main module must be safe to import: "protect the “entry point” of the program by using if name == 'main'".3
r10. exitcode is 0 when run() returns normally, and N when the child calls sys.exit() with an integer N. It is 1 for an uncaught exception, and -N when signal N ends the process.2
Examples
import sys
from multiprocessing import Process
def ok():
pass
def stop(code):
sys.exit(code)
if __name__ == "__main__":
procs = [Process(target=ok), Process(target=stop, args=(3,))]
for p in procs:
p.start()
for p in procs:
p.join()
print([p.exitcode for p in procs])
[0, 3]
import multiprocessing as mp
if __name__ == "__main__":
print(mp.get_start_method())
print(sorted(mp.get_all_start_methods()))
forkserver
['fork', 'forkserver', 'spawn']
This is the output on Linux with Python 3.14. On Windows and macOS, the default is spawn.1
from multiprocessing import Process
counter = 0
def add():
global counter
counter += 1
print("child:", counter)
if __name__ == "__main__":
p = Process(target=add)
p.start()
p.join()
print("parent:", counter)
child: 1
parent: 0
The child changes its own copy of counter (rule r5).
from multiprocessing import Process, Queue
def work(q):
q.put([42, None, "hello"])
if __name__ == "__main__":
q = Queue()
p = Process(target=work, args=(q,))
p.start()
print(q.get())
p.join()
[42, None, 'hello']
The parent calls get() before join(). The guidelines show that a join() first can deadlock when the child has put a large item that nobody has taken.3
from multiprocessing import Process, Value, Array
def change(n, a):
n.value = 3.1415927
for i in range(len(a)):
a[i] = -a[i]
if __name__ == "__main__":
num = Value("d", 0.0)
arr = Array("i", range(5))
p = Process(target=change, args=(num, arr))
p.start()
p.join()
print(num.value)
print(arr[:])
3.1415927
[0, -1, -2, -3, -4]
Versions
Since 3.14
On POSIX, the default start method changed from fork to forkserver "to retain the performance but avoid common multithreaded process incompatibilities." Fork "is no longer the default start method on any platform."1
Pitfalls
This code runs but gives a wrong answer on purpose.
from multiprocessing import Pool
if __name__ == "__main__":
with Pool(2) as pool:
try:
print(pool.map(lambda x: x * x, range(3)))
except Exception as e:
print(type(e).__name__)
PicklingError
A lambda has no importable name, so pickle cannot send it to a worker (rule r6). Define the function at the top level of a module.
A module that starts a process at import time fails with spawn and forkserver, because each child imports the main module again. Put the start code under if __name__ == "__main__": (rule r9).3
A program that sets no start method and depends on fork, for example on a global that the parent fills in before start(), can break on 3.14, where the default on POSIX is forkserver (rule r5). Pass the data as an argument, as the guidelines advise: "it is better to pass the object as an argument to the constructor for the child process."3
Comparison
multiprocessing.Process and threading.Thread have the same shape (rule r2), but a thread shares the memory of its process, and a process does not. Threads need locks for shared data; processes need pickling or shared memory. For a pool of workers with futures, The concurrent.futures module offers ProcessPoolExecutor on top of the same machinery. The documentation says it "offers a higher level interface to push tasks to a background process without blocking execution of the calling process."1
References
- multiprocessing — Process-based parallelism, Python 3.15 documentation, Introduction; The Process class; Contexts and start methods; Exchanging objects between processes; Sharing state between processes
- multiprocessing — Process-based parallelism, Python 3.15 documentation, Reference; Process and exceptions; exitcode; Process Pools
- multiprocessing — Process-based parallelism, Python 3.15 documentation, Programming guidelines
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag Q2K7C2 Edit this page History Report an error Markdown