Threads vs. Processes
Prerequisites: Concurrency and parallelism
A thread is a separate line of execution inside the same program, sharing memory with other threads. A process is a fully separate program instance, with its own memory.
Python has a detail worth knowing here: the Global Interpreter Lock
(GIL) means only one thread runs Python bytecode at a time, even on a
multi-core machine. Threads still help for I/O-bound work (waiting on
files/network), but don't give real parallelism for CPU-heavy work — use
multiprocessing (separate processes, each with its own GIL) for that
instead.
from multiprocessing import Pool
def square(n):
return n * n
with Pool(4) as pool:
print(pool.map(square, [1, 2, 3, 4]))
Expected output
[1, 4, 9, 16]