Multiprocessing in Python
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.
import multiprocessing
Brings in the module, part of the standard library.
Process(target=function)
Creates a new process that will run function, similar in shape to creating a thread.
.start() and .join() work the same way
As with threading, starting the process and waiting for it to finish.
Separate memory needs a shared tool
A Queue or a Pipe, rather than a plain shared variable, since each process has entirely separate memory.
Genuine parallelism across CPU cores
Unlike threading, which the GIL limits to one thread executing Python at a time.
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))Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Python yourself — building beats watching.