Advanced Topics
Python Internals
How CPython works under the hood — bytecode, GIL, PVM
Interview: Deep understanding — distinguishes senior engineers
Understanding CPython internals helps you reason about performance, concurrency limitations, and debugging. CPython is the reference implementation used by ~95% of Python developers.
The Execution Pipeline
Python source code goes through three stages: Source → Bytecode → PVM execution. The compiler converts .py files into bytecode (.pyc files cached in __pycache__/). The Python Virtual Machine (PVM) then executes bytecode instructions one at a time.
The Global Interpreter Lock (GIL)
The GIL is a mutex that allows only one thread to execute Python bytecode at a time. This means CPU-bound threads don't get true parallelism in CPython. However, the GIL is released during I/O operations and C extension calls.
GIL-Free Python (Python 3.13+)
Python 3.13 introduced an experimental --disable-gil build flag that removes the GIL entirely, enabling true multi-threaded parallelism. This is still experimental but represents a fundamental shift in Python's concurrency model.
Bytecode Inspection
You can inspect the bytecode of any function using the dis module. This reveals exactly what the PVM executes and helps understand why certain patterns are faster.
Use Cases
Understanding why threading does not help CPU-bound Python code
Using dis module to optimize hot paths by reducing bytecode
Choosing between threading (I/O-bound) and multiprocessing (CPU-bound)
Debugging performance issues with knowledge of PVM behavior
Common Mistakes
Using threading for CPU-bound work expecting parallelism
Not understanding that the GIL is released during I/O operations
Confusing the GIL with thread safety — you still need locks for shared state
Ignoring the overhead of multiprocessing (serialization, memory duplication)