concurrent.futures provides high-level interfaces for threading and multiprocessing.
1from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor, as_completed2import time34def io_task(url):5 time.sleep(1) # Simulate I/O6 return f"Result from {url}"78def cpu_task(n):9 return sum(i * i for i in range(n))1011# ThreadPoolExecutor — for I/O-bound12with ThreadPoolExecutor(max_workers=5) as executor:13 futures = [executor.submit(io_task, f"url{i}") for i in range(10)]14 for future in as_completed(futures):15 print(future.result())1617# ProcessPoolExecutor — for CPU-bound18with ProcessPoolExecutor(max_workers=4) as executor:19 results = list(executor.map(cpu_task, [10**6] * 4))20 print(results)2122# Future object23future = executor.submit(cpu_task, 10**6)24print(future.done()) # False (or True)25print(future.result()) # Blocks until done26print(future.cancel()) # Cancel if not started
Benefits:
as_completed() for streaming results.