Home > Article > Backend Development > Implementation method and principle analysis of GIL global interpreter lock in Python
The designers want to avoid complex competing risk issues (race conditions) similar to memory management
Because CPython uses a large number of C language libraries, but most of them C language libraries are not natively thread-safe (thread safety will reduce performance and increase complexity)
When multiple threads execute, each thread starts When executing, the GIL will be locked to prevent other threads from executing. Similarly, after each thread completes a period of execution, the GIL will be released to allow other threads to start using resources.
There is another one in CPython Mechanism, called check_interval, the CPython interpreter will poll and check the lock status of the thread GIL. Every once in a while, the Python interpreter will force the current thread to release the GIL so that other threads can have a chance to execute
for (;;) { if (--ticker < 0) { ticker = check_interval; /* Give another thread a chance */ PyThread_release_lock(interpreter_lock); /* Other threads may run now */ PyThread_acquire_lock(interpreter_lock, 1); } bytecode = *next_instr++; switch (bytecode) { /* execute the next instruction ... */ } }
With GIL, you still need to consider thread safety when programming
Bypass CPython and use other implementations such as JPython (Python interpreter implemented in Java)
Put the key performance code into other languages (usually C) to implement
GIL adopts a mechanism of running threads in turns. GIL needs to continuously switch between threads. If there are many threads or the running time is long, the performance loss caused by switching may exceed that of a single thread
In fact, after Python 3, there is indeed a lot of discussion about the improvement or even cancellation of GIL. What is your opinion? Have you ever been troubled by GIL in your daily work?
GIL is still a good design. Although it loses performance, it plays a certain role in ensuring that resources will not conflict and prevent deadlocks
The above is the detailed content of Implementation method and principle analysis of GIL global interpreter lock in Python. For more information, please follow other related articles on the PHP Chinese website!