When would you use threading vs asyncio vs multiprocessing?hard
For I/O-bound work like API calls or DB queries, use asyncio or threading. For CPU-bound work like data processing or model inference, use multiprocessing — each process gets its own GIL, enabling true parallelism. asyncio runs one thread with an event loop that switches between coroutines at every await.
threading uses OS-level threads with shared memory, limited by the GIL for CPU work but useful for I/O. multiprocessing spawns separate OS processes with their own interpreter, truly parallel on multiple cores but with higher memory overhead since processes don't share state by default. asyncio is single-threaded cooperative multitasking — coroutines yield control at await points, making it ideal for high-concurrency I/O with zero thread overhead. concurrent.futures provides a unified high-level API over both thread and process pools via ThreadPoolExecutor and ProcessPoolExecutor.