Python Tutorial 0/52 lessons ~6 min read Lesson 40
Threads, Processes & GIL
Python's GIL (Global Interpreter Lock) means threads can't run Python bytecode in parallel — use multiprocessing or concurrent.futures for CPU-bound work.
Course progress0%
Focus
9 guided sections
Practice signal
Examples included
Career prep
Foundation builder
Introduction
Python's GIL (Global Interpreter Lock) means threads can't run Python bytecode in parallel — use multiprocessing or concurrent.futures for CPU-bound work.
Understanding the topic
Core concepts to understand:
- GIL = one Python instruction at a time per process.
- Threads good for I/O, bad for CPU.
multiprocessingbypasses the GIL.concurrent.futuresis the high-level API.
Syntax reference
Visual flow / code:
python
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor# I/O-bound: threadswith ThreadPoolExecutor(max_workers=8) as pool:results = list(pool.map(fetch_url, urls))# CPU-bound: processeswith ProcessPoolExecutor() as pool:results = list(pool.map(heavy_compute, data))
Execution workflow
1Threads, Processes & GIL Workflow
1 / 4Step 1
GIL = one Python instruction at a time per process.
Apply this step while implementing threads, processes & gil in real code.
Real-world use
Python 3.13 introduces an experimental no-GIL build (PEP 703). For now: threads for I/O, processes for CPU, async for thousands of connections.
Best practices
- Threads for I/O.
- Processes for CPU.
- Async for very-high-fanout I/O.
Common mistakes
- Trying to speed up NumPy with threads — usually works (releases GIL), but pure Python doesn't.
Hands-on exercise
Interview preparation — practice these questions:
- What is the GIL?
- Threads vs processes vs async — when?
- Will Python remove the GIL?
Summary
In summary: GIL serializes Python bytecode. Pick the right tool for the workload.
Ready to mark this lesson complete?Track your journey across the entire course.