Understanding the Global Interpreter Lock (GIL) and How to Bypass It

import time
import threading
import multiprocessing

def cpu_task(n):
total = 0
for _ in range(n):
total += 1
return total

N = 10**7 # Large computation

# Using threads (affected by GIL)
start = time.time()
threads = [threading.Thread(target=cpu_task, args=(N,)) for _ in range(4)]
for t in threads: t.start()
for t in threads: t.join()
print(f”Threads Time: {time.time() – start:.4f} sec”) # No significant speedup

# Using multiprocessing (bypasses GIL)
start = time.time()
processes = [multiprocessing.Process(target=cpu_task, args=(N,)) for _ in range(4)]
for p in processes: p.start()
for p in processes: p.join()
print(f”Processes Time: {time.time() – start:.4f} sec”) # Much faster

# Save as `nogil_example.pyx`
cdef double compute(int n) nogil:
cdef int i
cdef double result = 0
for i in range(n):
result += i ** 0.5
return result

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top