Python multithreading (a): GIL

Python multithreading (a): GIL

Now learning Python, multithreaded programming, to consolidate the knowledge about the point

  • GIL is the Global Interpreter Lock, namely the full name is an acronym for the global interpreter lock to ensure that only one thread runs on cpu byte code, it can not be mapped to multiple threads on multiple CPU. This is ** CPython interpreter defects ** due Cpython is the default under most environmental execution environment, and a lot of pain are based on CPython written, so many people will be attributed to the GIL python problem
  • GIL is designed to protect the security thread , a number of threads share global resources, if not well synchronized to thread, multi-thread is very easy to make an error of data resources, in fact, even with the GIL also can not completely solve the problem, because GIL will also release a lock according to the conditions, then the thread is not yet finished, another thread has already started, it is still very easy to be a data resource error
  • When the program starts running, it creates a process, GIL is in the process of a Global Interpreter Lock, multiple threads can simultaneously to compete for the lock, who grabbed, will come to perform, did not grab would need to wait. when the process thread execution in certain conditions are met ( eg: the I O, time-consuming operations, or byte code chip time **) ** time, the lock will release the GIL, other threads are waiting for this to continue snatch lock, because the thread is a shared global variable, so when the data operation is likely to destroy the data, the data error occurred, this time the solution is to use thread synchronization, such as using a mutex to ensure data accuracy
  • Mutex: use mutex purpose is in the implementation of certain key code can only be executed from start to finish to complete by a thread , but the drawback is affecting the efficiency of the code, into a single multi-tasking task.
发布了11 篇原创文章 · 获赞 9 · 访问量 229

Guess you like

Origin blog.csdn.net/lzn1210899799/article/details/103622870
GIL