Robots Atlas>ROBOTS ATLAS

Python — From Basics to Advanced · Concurrency II — Threads, Processes, GIL

concurrent.futures — Abstraction Over Threads and Processes

Concurrency II — Threads, Processes, GIL

Introduction

concurrent.futures (3.2+) is the modern, idiomatic API over threading and multiprocessing. Two executors: ThreadPoolExecutor (thread worker pool) and ProcessPoolExecutor (process worker pool) with identical interfaces. The choice between them is the choice between I/O concurrency (threads) and CPU parallelism (processes).

Core API: submit(fn, *args, **kwargs) returns a Future, map(fn, iterable, timeout=, chunksize=) like built-in map but parallel, shutdown(wait=True) closes the pool. Best practice: always use with ExecutorClass(...) as ex:__exit__ calls shutdown(wait=True) automatically.

Future represents a pending result: result(timeout) blocks until ready (or re-raises a worker exception), exception() returns the exception without raising, done()/running() check state, cancel() attempts cancellation (rarely works for running tasks), add_done_callback(fn) registers a callback. Helpers: as_completed(futures) iterates in completion order, wait(futures, return_when=) with FIRST_COMPLETED/ALL_COMPLETED/FIRST_EXCEPTION.