The Global Interpreter Lock (GIL)
00:00 CPython’s approach to thread safety is to use what is called the Global Interpreter Lock, or GIL. Rather than use a bunch of locks on individual shared resources, which would be very hard for compatibility and readability reasons, the GIL locks the entire Python interpreter.
00:20 Remember, that’s the part of CPython that reads in the bytecode and carries out the associated operation. Because the entire interpreter is locked, instead of a specific shared resource, each thread must obtain the interpreter lock to interpret some bytecode.
00:37 And because only one thread can have the lock at any given time, this essentially makes CPU-bound code—trying to perform a computation with multiple threads—singly-threaded. However, threading does have uses in other places of Python, like user interfaces and IO, but that’s not the focus here.
00:58 This performance limitation on CPU-bound code makes the GIL particularly infamous in the Python community. It’s a simple approach that allows Python to be both readable and compatible with old C extension modules, but at the cost of performance.
Become a Member to join the conversation.