QA
Beginner LevelPython

What is the Python Global Interpreter Lock (GIL) and how does it affect multi-threading?

❓ Interview Prompt: Explain what the GIL is in CPython, why it prevents true parallel execution of CPU-bound threads, and how multiprocessing solves it.

The Global Interpreter Lock (GIL) is a mutex (mutual exclusion lock) used by CPython (the standard Python implementation) that allows only one thread to execute Python bytecode at a time, even on multi-core processors.

Why does the GIL exist?

CPython's memory management uses reference counting. When an object's reference count drops to zero, it is garbage collected. If multiple threads simultaneously increment or decrement reference counts without locking, memory corruption and race conditions occur. The GIL was the simplest, fastest solution to make CPython thread-safe.

Impact on Multi-Threading:

•
CPU-Bound Tasks (Math, Image Processing): Multi-threading in Python does NOT speed up CPU-bound tasks! Because of the GIL, threads take turns executing on a single core, actually running slower due to context switching overhead.
•
I/O-Bound Tasks (Network Requests, File I/O, Database Queries): Multi-threading does work for I/O-bound tasks! When a thread waits for network data or disk I/O, it releases the GIL, allowing other Python threads to run concurrently.

How to Achieve True Parallelism:

To utilize multiple CPU cores for heavy calculations, use the 'multiprocessing' module (which spawns separate OS processes, each with its own memory space and GIL) or Python 3.13+ Free-Threading / GIL-less mode ('--disable-gil').

Code Implementation Example

python
import time
from threading import Thread
from multiprocessing import Process

def heavy_countdown(n):
    while n > 0:
        n -= 1

# 1. Multi-Threading (2 Threads running on 1 Core due to GIL!)
start = time.time()
t1 = Thread(target=heavy_countdown, args=(50_000_000,))
t2 = Thread(target=heavy_countdown, args=(50_000_000,))
t1.start(); t2.start()
t1.join(); t2.join()
print('Threading Time:', round(time.time() - start, 2), 'sec')

# 2. Multiprocessing (2 Processes running in parallel on 2 CPU cores!)
start = time.time()
p1 = Process(target=heavy_countdown, args=(50_000_000,))
p2 = Process(target=heavy_countdown, args=(50_000_000,))
p1.start(); p2.start()
p1.join(); p2.join()
print('Multiprocessing Time:', round(time.time() - start, 2), 'sec')
💡Benchmarking CPU-bound execution times across Threading and Multiprocessing.
🎯 Interview Key Takeaway

The GIL limits Python threads to 1 CPU core. Use threading for I/O-bound tasks and multiprocessing for CPU-bound tasks.

#GIL#Threading#Multiprocessing#Beginner
Back to all Python Questions→

More Python Interview Questions

Beginner LevelPython

What is the difference between a List, Tuple, Set, and Dictionary in Python?

Beginner LevelPython

Why are mutable default arguments (like def func(items=[])) dangerous in Python and how do you fix them?

Beginner LevelPython

What is the difference between a List Comprehension and a Generator Expression in Python?