Implementation method and principle analysis of GIL global interpreter lock in Python

PHPz
Release: 2023-04-26 15:16:08
forward
1461 people have browsed it

1. Why is there GIL

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)

2. How GIL works

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 ... */ 
    }
}
Copy after login

3.Thread safety of python

With GIL, you still need to consider thread safety when programming

4.How to bypass GIL?

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

5. When processing CPU-intensive tasks, why is multi-threading sometimes slower than single-threading?

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

6. Do you think GIL is a good design?

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template