Trung BìnhPython iconPython

Threading vs Multiprocessing vs Asyncio — khi nào dùng cái nào?

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 threading cho I/O-bound với thư viện blocking không hỗ trợ async.
  • Dùng multiprocessing cho 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).

Pitfall: asyncio không giúp gì cho CPU-bound — dùng ProcessPoolExecutor kết hợp với asyncio.

Xem toàn bộ Python cùng filter theo level & chủ đề con.

Mở danh sách Python