Q21: What is the Global Interpreter Lock (GIL) and how does it affect multi-threading?
The GIL is a mutual exclusion lock in CPython preventing multiple native threads from executing Python bytecode simultaneously on separate CPU cores. Thus, multi-threading benefits I/O-bound applications, while CPU-bound tasks require multiprocessing to bypass the GIL.
Key Takeaways
- Prevents multi-core execution of CPython bytecode across threads.
- Use
threadingfor I/O-bound tasks; usemultiprocessingor C-extensions for CPU-bound tasks.
Knowledge Check
Which module bypasses the GIL for CPU-heavy mathematical calculations by spawning independent OS processes?