Repaso
Español
PythonIntermediatePY-41

Explain the GIL and how you would speed up a CPU-bound Python job.

Answer

  • GIL: a lock in CPython; only one thread executes Python code at a time. It protects reference counts and interpreter state.
  • It is released during blocking I/O and by many C extensions (NumPy, hashlib, zlib).
  • I/O-bound work: threads or asyncio work well.
  • CPU-bound options: ProcessPoolExecutor / multiprocessing; vectorize with NumPy/pandas; move the hot loop to Cython/Rust/C; free-threaded build (3.13+) where libraries support it.
  • Trade-offs: process start-up, pickling cost, memory duplication. Profile first (cProfile, py-spy).

Key terms

Practice this question in Repaso