all Python 3multiprocessingterm
multiprocessing
module
Parallel work in several processes: sidesteps the GIL for CPU-bound tasks, with pools, queues and shared memory.
Syntax
import multiprocessing
Use multiprocessing for CPU-bound work: threads from threading share one GIL, processes do not. For a plain "hand work to a pool" job, concurrent.futures.ProcessPoolExecutor is simpler. Start-up code must sit under if __name__ == '__main__': with spawn (the default on Windows and macOS) and forkserver (the default on Linux since 3.14) the child re-imports the main module. Functions and data you pass must be picklable, so a lambda will not work.
Examples
from multiprocessing import Pool
def square(x):
return x * x
if __name__ == '__main__':
with Pool(2) as pool:
print(pool.map(square, [1, 2, 3])) # → [1, 4, 9]
See also
Open in the full glossarydocs.python.orgReport an inaccuracy