The concurrent.futures module in the Python standard library gives you a high-level API for running callables asynchronously on a pool of threads or processes. You submit work to an executor (ThreadPoolExecutor or ProcessPoolExecutor) and get back Future objects that let you wait for results, handle exceptions and cancel pending work.
Use threads for I/O-bound work such as HTTP requests or file access, and processes for CPU-bound work that would otherwise be held back by the global interpreter lock (GIL).
concurrent.futures.Executor is the abstract base class. It has two concrete implementations:
| Class | Runs tasks in | Best for | Arguments and results |
|---|---|---|---|
ThreadPoolExecutor | Worker threads in one process | I/O-bound tasks (network, disk, waiting) | Shared memory, no pickling |
ProcessPoolExecutor | Separate worker processes | CPU-bound tasks | Must be picklable |
A Future represents a result that may not exist yet. Its main methods are result(timeout=None), exception(), done(), cancel() and add_done_callback(fn). You normally do not create futures yourself. executor.submit(fn, *args, **kwargs) creates and returns one.
Always use an executor as a context manager. Leaving the with block calls shutdown(wait=True), which waits for pending work and releases the workers.
The four one-second tasks overlap, so the whole run takes about one second instead of four. The order of the lines can vary, because as_completed yields futures in completion order.
If you only need results in input order, use map:
map returns results in the order of the inputs, and an exception raised by a task is re-raised when you reach its result in the iteration.
Processes each have their own interpreter and GIL, so CPU-heavy functions can use several cores.
The if __name__ == "__main__": guard is required on platforms that start workers with spawn (Windows and macOS). Without it, each worker re-imports the module and tries to create more workers. Functions and arguments must be picklable, so lambdas and locally defined functions do not work. Since Python 3.14 the default start method on Linux is no longer fork, which makes the guard important there too.
An exception inside a task is stored on its future and raised when you call result().
Mapping each future to its input, as the dictionary does here, is the standard way to know which task failed. Note that result(timeout=...) only stops waiting. It does not stop the running task. future.cancel() works only for tasks that have not started yet. To stop pending tasks at shutdown, call pool.shutdown(cancel_futures=True).
concurrent.futures.wait(futures, timeout=None, return_when=...) returns a done and not_done set, and return_when can be FIRST_COMPLETED, FIRST_EXCEPTION or ALL_COMPLETED.
concurrent.futures when you have a batch of independent calls and want simple pool management.asyncio for very large numbers of concurrent network connections, with loop.run_in_executor or asyncio.to_thread to call blocking code from async code.threading module only when you need custom thread lifecycle control.multiprocessing directly for shared-memory or long-lived worker designs.Python 3.13 introduced an optional free-threaded build (no GIL), and Python 3.14 officially supports it. In that build threads can run Python code in parallel, but the default build still has the GIL, so check how your interpreter was built before replacing processes with threads.
submit schedules one call and returns a Future, so you control how results are collected. map schedules a call for every item and yields results in input order.
For ProcessPoolExecutor, the default is the number of CPUs, which is a good starting point. For ThreadPoolExecutor, the default is min(32, os.cpu_count() + 4), and you can raise it for tasks that mostly wait on I/O.
No. In the default CPython build only one thread runs Python bytecode at a time. Threads help when tasks spend their time waiting, for example on network or disk I/O.
Yes. Call await loop.run_in_executor(pool, func, *args) or await asyncio.to_thread(func, *args) to run blocking functions without freezing the event loop.
concurrent.futures offers one interface for thread and process pools. Use ThreadPoolExecutor for I/O-bound work, ProcessPoolExecutor for CPU-bound work, as_completed or map to collect results, and always handle exceptions through Future.result(). Use the pool as a context manager so workers shut down cleanly.