InterviewHubPRO
Section Overview
Q7

Question 7 of 65

Part 2: Python Core Language, Internals & Runtime (6 - 25)

11%
Python & FastAPI•Part 2: Python Core Language, Internals & Runtime (6 - 25)

What is the Global Interpreter Lock (GIL) and free-threaded Python?

In Simple Words: The GIL is a mutex in CPython that ensures only one native thread executes Python bytecode at a time, protecting CPython memory management from race conditions.

  • Impact: Multi-threaded Python cannot run CPU-bound tasks in parallel across multiple CPU cores.
  • Python 3.13+ Free-threaded Mode (PEP 703): Introduces an experimental build that removes the GIL for true multi-threaded CPU parallelism.
python
# To achieve true multi-core CPU parallelism in standard CPython:
from concurrent.futures import ProcessPoolExecutor

def cpu_heavy_task(n):
    return sum(i * i for i in range(n))

if __name__ == '__main__':
    with ProcessPoolExecutor() as executor:
        results = list(executor.map(cpu_heavy_task, [10_000_000, 10_000_000]))