When would you use threading vs asyncio vs multiprocessing?hard

Type
conceptual
Topic
concurrency
Frequency
common
Tags
threading, asyncio, multiprocessing, concurrency, GIL
Answer

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.

Explanation

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.

Follow-upYou need to call 50 external APIs simultaneously. What do you use and why?
Follow-upHow does concurrent.futures unify threading and multiprocessing?
Follow-upWhy does asyncio handle thousands of connections with just one thread?