Global Interpreter: GIL Lock

Gil

Global interpreter lock, each thread needs to acquire Gil first during execution to ensure that only one thread can execute code at the same time

  • Interview questions:

Describe the concept of the Python GIL, and how it affects multithreading in python? Write a program that crawls web pages with multiple threads, and clarify whether the performance of a multi-threaded program can be improved compared to a single-threaded program, and explain why.

The gil lock in python is the global interpreter lock, which ensures that a thread can use the cpu at the same time, due to the appearance of the cpythont interpreter. Only one thread uses the CPU at the same time, which means that the CPU is not used 100%. The multi-threaded crawling is better than the single-threaded performance, because the gil lock will be automatically released when it encounters io blocking.

Gil's extension

Function: Ensure that only one thread can use the CPU at the same time. Explanation: When we use multiple threads, there is only one GIL lock in a process, then whoever gets the GIL among the multiple threads can use the cpu (ps: multiple process has multiple Gil locks)

Question 1: When will the Gil lock be released,

  • 1 Gil will be released when the cpu is idle due to idle time such as i/o operations.
  • 2 There will be a special ticks for counting. Once the ticks value reaches 100, the Gil lock will be released and the Gil lock will be competed between threads (Explanation:
  • The value of ticks can be set to extend or reduce the time that the thread that acquires the Gil lock uses the cpu)

Question 2: The relationship between mutex and Gil lock

  • Gil lock: guarantees that only one thread can use the cpu at the same time

  • Mutual exclusion lock: When multi-threaded, ensure orderly modification when modifying shared data, without causing confusion in data modification

  • First assume that there is only one process, there are two threads Thread1, Thread2 in this process, to modify the shared data date, and there is a mutual exclusion lock

Perform the following steps

  • (1) Multi-threaded operation, assuming that Thread1 obtains the GIL and can use the cpu, then Thread1 obtains the mutex lock, and Thread1 can change the date data (but does not start to modify the data)

  • (2) The Thread1 thread has an i/o operation or the ticks count reaches 100 before modifying the date data (note that it has not run to modify the data data), at this time Thread1 gives up Gil, and the Gil lock can be competed

  • (3) Thread1 and Thread2 start to compete for Gil (Note: If Thread1 is the Gil that is given up because of i/o blocking, Thread2 must get Gil, if Thread1 is giving up Gil because the ticks count is over 100, then Thread1 and Thread2 compete fairly)

  • (4) Assuming that Thread2 has just obtained the GIL, run the code to modify the shared data date. Since Thread1 has a mutex lock, Thread2 cannot change the shared data date. At this time, Thread2 gives up the Gil lock, and the GIL lock competes again.

  • (5) Assuming that Thread1 grabs the GIL again, because it has a mutex lock, it can continue to modify the shared data data. When Thread1 modifies the data and releases the mutex lock, Thread2 can modify the data only after obtaining the GIL and lock.

Guess you like

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