Q72: How does the 'concurrent.futures' module simplify parallel execution?

concurrent.futures supplies high-level execution pool abstractions:

  • ThreadPoolExecutor: Manages worker threads for I/O tasks.
  • ProcessPoolExecutor: Manages worker processes for CPU tasks. Both return Future objects supporting non-blocking result() and add_done_callback() calls.
ProcessPoolExecutor Parallel Processing
from concurrent.futures import ProcessPoolExecutor

def heavy_task(x):
    return x * x

if __name__ == '__main__':
    with ProcessPoolExecutor(max_workers=4) as executor:
        results = list(executor.map(heavy_task, range(100)))
💡 ProcessPoolExecutor distributes tasks across process pools using map.
Key Takeaways
  • High-level thread and process pool executor API.
  • Returns Future objects representing background task execution.
Knowledge Check

Which method on a concurrent.futures.Future object blocks execution until the background result is ready?