Learn Python
>>> advanced_python.multiprocessing

Multiprocessing in Python

#concept

Multiprocessing runs separate, fully independent Python processes, each with its own memory space and its own Python interpreter, entirely sidestepping the Global Interpreter Lock's one-thread-at-a-time limitation. This makes it the right tool for genuinely CPU-bound work — heavy computation that multithreading can't actually speed up — at the cost of more overhead and no automatically shared memory between processes.

#how it works
1

import multiprocessing

Brings in the module, part of the standard library.

2

Process(target=function)

Creates a new process that will run function, similar in shape to creating a thread.

3

.start() and .join() work the same way

As with threading, starting the process and waiting for it to finish.

4

Separate memory needs a shared tool

A Queue or a Pipe, rather than a plain shared variable, since each process has entirely separate memory.

5

Genuine parallelism across CPU cores

Unlike threading, which the GIL limits to one thread executing Python at a time.

#example
import multiprocessing
 
def square(n, results, index):
results[index] = n * n
 
if __name__ == "__main__":
numbers = [2, 3, 4]
manager = multiprocessing.Manager()
results = manager.list([0, 0, 0])
 
processes = []
for i, n in enumerate(numbers):
p = multiprocessing.Process(target=square, args=(n, results, i))
processes.append(p)
p.start()
 
for p in processes:
p.join()
 
print(list(results))
Output
[4, 9, 16]
1Each number gets its own separate process, computing its square entirely independently, potentially on a different CPU core.
2manager.list(...) provides a special shared list that different processes can all safely write into, since a plain Python list wouldn’t automatically be shared across separate process memory spaces.
3Waiting for every process with .join() ensures all three squares have actually been computed before reading the final, complete results.
4The final list shows all three results correctly gathered back together, even though the actual squaring happened in three separate processes.
#use cases
Speeding up genuinely CPU-heavy work, like image processing or numerical simulations, across multiple CPU cores
Working around the GIL's one-thread-at-a-time limitation for computation-heavy tasks
Running several independent, isolated tasks that shouldn't be able to affect each other's memory
Choosing between multithreading (for I/O-bound waiting) and multiprocessing (for CPU-bound computing), the central decision this pair of topics sets up

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Python yourself — building beats watching.