NewWork mode: your Claude Code or Codex on work calls, answering from your project's codeSkip to content
Senior0 views

How does multithreading differ from multiprocessing in Python, and when would you choose threads, processes, or asyncio?

Practice

AI answer

Threads run within a single process and share memory, so they are convenient for I/O-bound tasks—for example, network requests or waiting for files. Shared state must be synchronized. In CPython, the GIL usually prevents multiple threads from executing Python bytecode simultaneously, so threads rarely speed up CPU-bound code.

Processes have separate memory and avoid this limitation, making them suitable for CPU-bound computations and task isolation. The tradeoff is more overhead for startup and data exchange.

I choose asyncio for a large number of concurrent I/O operations when the libraries in use support an async API. It works efficiently with cooperative task switching, but a blocking call will stall the entire event loop; CPU-bound work generally requires processes or a separate executor.