concurrent.futures — high-level interface for async execution.
1from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor, as_completed2import time34def fetch(url):5 time.sleep(1) # Simulate network6 return f"Data from {url}"78# ThreadPoolExecutor9urls = ["url1", "url2", "url3", "url4"]1011with ThreadPoolExecutor(max_workers=4) as executor:12 # Submit and get futures13 futures = {executor.submit(fetch, url): url for url in urls}1415 # Process as completed16 for future in as_completed(futures):17 url = futures[future]18 try:19 result = future.result()20 print(f"{url}: {result}")21 except Exception as e:22 print(f"{url} failed: {e}")2324# ProcessPoolExecutor for CPU-bound25with ProcessPoolExecutor() as executor:26 results = list(executor.map(cpu_bound, data_chunks))
Key features: