Search GimmeJob

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

What does the `concurrent.futures` module provide, and how do `ThreadPoolExecutor` and `ProcessPoolExecutor` differ in usage?

Answer

`concurrent.futures` gives a unified, higher-level API for submitting callables to a pool of workers and getting back `Future` objects that represent results still being computed, replacing a lot of manual thread- or process-management boilerplate. `ThreadPoolExecutor` and `ProcessPoolExecutor` share the same core `Executor`/`Future` interface — `submit()`, `map()`, and `as_completed()` — so switching between thread-based and process-based execution for a given workload is often a small change, though each executor keeps its own constraints that the shared interface does not remove (picklable arguments and return values for processes; no true CPU parallelism for threads on a default GIL-enabled interpreter). Whether threads actually gain CPU parallelism depends on the interpreter build: the default GIL-enabled CPython build does not give threads true parallel CPU execution, but Python 3.14's officially supported free-threaded build does.

Strong answer includes

  • unified API returning Future objects from a worker pool
  • ThreadPoolExecutor and ProcessPoolExecutor share the core Executor/Future interface but keep their own constraints
  • CPU parallelism for threads depends on the interpreter build — default GIL build vs free-threaded 3.14

Practical example

Python example
with ThreadPoolExecutor(max_workers=8) as pool:
    results = list(pool.map(fetch_url, urls))

becomes CPU-parallel just by swapping `ThreadPoolExecutor` for `ProcessPoolExecutor` , as long as `fetch_url` and its arguments are picklable.

#concurrent-futures#threading#multiprocessing

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 communication