Dùng asyncio cho I/O-bound với nhiều concurrent operations (web APIs, DB queries) — single thread, cooperative concurrency, overhead thấp nhất.
- Dùng
threadingcho I/O-bound với thư viện blocking không hỗ trợ async. - Dùng
multiprocessingcho CPU-bound (ML training, image processing, data transformation) — bypass GIL, chạy trên nhiều CPU cores thực sự, nhưng overhead cao hơn (IPC, memory). - Lưu ý:
asynciokhông giúp gì cho CPU-bound — dùngProcessPoolExecutorkết hợp với asyncio.
Use asyncio for I/O-bound with many concurrent ops (web APIs, DB) — single thread, lowest overhead.
- Use
threadingfor I/O-bound with blocking libraries. - Use
multiprocessingfor CPU-bound (ML, image processing) — bypasses GIL, true parallelism.
Pitfall: asyncio does not help CPU-bound work — combine with ProcessPoolExecutor.