Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
Python interview question
Concurrency & parallelismMiddleVery commonDesign

How do you decide between `threading` and `multiprocessing` for a given workload?

Answer

Use `threading` for I/O-bound work — network calls, file access, database queries — where most of the time is spent waiting, because in the default GIL-enabled CPython build the GIL is released during blocking I/O and threads are lightweight to create with shared memory. Use `multiprocessing` for CPU-bound work — heavy computation, image processing, data crunching — because each process gets its own Python interpreter and its own GIL, giving genuine parallel execution across cores, at the cost of higher startup overhead, no shared memory by default, and the requirement that data passed between processes be picklable. This split is the usual heuristic for the default CPython build, not an absolute rule: Python 3.14's officially supported free-threaded build lets CPU-bound Python threads run genuinely in parallel too, and many native extensions (NumPy and other C-accelerated libraries) release the GIL during their own CPU-heavy work even on the default build.

Strong answer includes

  • threading for I/O-bound work, GIL releases during blocking calls in the default build
  • multiprocessing for CPU-bound work, separate interpreter and GIL per process
  • the split is a default-build heuristic, not absolute — free-threaded 3.14 and GIL-releasing extensions are exceptions

Practical example

Choose an executor from the workload
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor

def fetch(url):
    ...  # blocking network I/O

def crunch(number):
    return sum(i * i for i in range(number))

with ThreadPoolExecutor(max_workers=8) as pool:
    io_results = list(pool.map(fetch, urls))

with ProcessPoolExecutor() as pool:
    cpu_results = list(pool.map(crunch, numbers))

Threads are the usual fit for blocking I/O because waiting can overlap cheaply. Processes are the usual default-build CPython choice for pure-Python CPU work because each process has its own interpreter.

Expected result: The example illustrates the model choice; fetch/urls/numbers must be supplied by the application.

#threading#multiprocessing#design

Sources

threading — Thread-based parallelismPython Software Foundation · Locks, thread safety and thread-local statemultiprocessing — Process-based parallelismPython Software Foundation · Process pools, pickling constraints and inter-process communicationPython Interview Questions and AnswersToptal · Senior-level coverage signal for concurrency, testability and design-quality questions