Threads: I/O-bound work with blocking libraries (e.g.
1 2 3 4 5 6 7 8 9 10 11 12 13 | # ====================================================================== # Q: Threads vs processes vs asyncio: when to use each? # # Threads: I/O-bound work with blocking libraries (e.g. 20 HTTP calls with # requests via ThreadPoolExecutor). Processes: CPU-bound work (calculations, # images), to bypass the GIL. asyncio: thousands of concurrent I/O connections # (WebSockets, many external APIs) with async libraries (httpx, asyncpg). # ====================================================================== from concurrent.futures import ThreadPoolExecutor with ThreadPoolExecutor(max_workers=10) as pool: prices = list(pool.map(fetch_supplier_price, supplier_urls)) |