GIL

Knowledge reserve:

 

"""
 1. What is the GIL

  The global interpreter lock is essentially a mutex lock, which is added to the interpreter. There is such a lock i in every python process.

2. What impact will the GIL have on multiple threads under a single process?

  If a multi-thread wants to execute its own code, it first needs to compete for the GIL. For all threads to be executed, the GIL is equivalent to the execution permission.

  Only one thread competes successfully at the same time, that is, only one multi-threaded thread under a single process is running at the same time

  It means that the multithreading under the process does not have the effect of parallelism, but has the effect of concurrency

  PS: Threads scattered in different processes will not compete for the same GIL, only multiple threads of the same process will compete for the same GIL

3. Why do we need GIL?

  The memory management mechanism of the Cpython interpreter (garbage collection threads) is not "thread safe"

4. Similarities and differences between GIL and custom Hu Zhisuo, analysis of the process of multiple threads competing for GIL and custom mutex

  same:

    Mutex
  difference:

    The GIL is added to the interpreter and acts globally
    Custom mutex acts locally
    
    All threads in a single process will grab the GIL
    Only a part of the threads in a single process will grab the custom mutex
5. When to use python's multi-threading, when to use multi-process, and why?

  Multi-threaded threads under a single process cannot be parallelized, which means that the advantages of multi-core cannot be used.

  CPU is to perform computing work, multiple CPUs mean to improve computing performance

  The CPU cannot do I /O operations, which means that the performance benefits of multiple CPUs in the face of I/O operations are minimal.

  

  When the program is I / O intensive, the performance improvement of multi-core is negligible, and python's multi-threading can be used at this time.

  When the program is computationally intensive, you must use the multi-core advantage. At this time, you can use python multi-process

    

** Analysis of the process of multiple threads competing for GIL and custom mutex locks**
    Multiple threads compete for the GIL lock first,

"""

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324955513&siteId=291194637
GIL