Sobes.tech
Junior — Middle

Who manages thread switching in the implementation of GIL in Python?

sobes.tech AI

Answer from AI

In the implementation of GIL (Global Interpreter Lock) in CPython, the thread switching is managed by the Python interpreter itself. GIL allows only one Python bytecode thread to execute at a time to avoid issues with concurrent access to objects.

Thread switching occurs based on a timer: the interpreter allocates a certain number of instructions (usually 5 milliseconds), after which it forcibly releases the GIL and allows another thread to acquire it. Thus, thread switching is a cooperative process controlled by the interpreter, not directly by the operating system.