Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
Use asyncio for many concurrent I/O operations with async libraries, threads for I/O with blocking libraries or small amounts of concurrency, and multiprocessing for CPU-bound work that needs multiple cores.
asyncio runs many coroutines on a single thread and switches at await points, so it scales to thousands of concurrent network connections cheaply, but every library in the path must be async and one blocking call stalls everything. Threads let blocking libraries run concurrently because the GIL is released during I/O, and concurrent.futures.ThreadPoolExecutor makes them simple to use, though shared state needs locks. Multiprocessing runs separate interpreters with their own GIL, giving real parallelism for CPU-heavy work, at the cost of process start-up and the need to pickle data sent between processes. Mixed systems often combine them, for example running CPU work in a ProcessPoolExecutor from asyncio.