and python GIL

First, what GIL is, why it exists

  python was probably the most criticized its global GIL lock, but today I want to justify it, it has nothing to do with python itself, but it has a relationship with the interpreter.

  We know, python code is well written, eventually running a .py file, the process of running the code, in fact, is to allow the interpreter to explain to our code procedural language machine can recognize. Can explain python code interpreter there are many, such as:

  • cpython (official interpreter)
  • ipython (based cpython an interpreter, interactive stronger, as with other cpython)
  • PyPy (using JIT (Just In Time) compiler compiler is instantly performed dynamically compiled on the Python code, object speed up execution)
  • jython (interpreter on the Java platform)
  • ironpython (with jython similar, but ironPython Python interpreter is running on the Microsoft .Net platform)

  The official explanation is cpython use, that is, when we downloaded from the official website to install python time, it has been used python interpreter is cpython default. The GIL is cpython explain python code in time to give it added a lock, GIL stands for Global Interpreter Lock (Global Interpreter Lock), so GIL and cpython, but not with python, you can not change a GIL interpreter .

  So GIL why it exists, explained the official website is: GIL is a mutually exclusive lock that prevents multiple threads execute Python byte code, this lock is necessary, mainly because the memory manager is not thread safe CPython's. A little dizzy. Further understand how the internal variables or data objects to Python using reference counter, we calculated the number of reference, when the number is 0, or a variable data object is automatically released. The reference number needs to be protected, multiple threads may simultaneously to modify it, it might cause a memory leak, then we lock it introduced, multiple locks could lead to deadlock, so in order to father once and for all, the Dutch python Guido van Rossum (Guido van Rossum) in the default interpreter CPython uses a single lock that the global interpreter lock GIL. In the implementation of Python bytecode need to get GIL, which can prevent a deadlock, but it also poses a problem, it effectively makes any Python program is single-threaded CPU-bound.

Two, GIL have any effect on coding

  GIL it effectively so that any Python program is single-threaded CPU-bound. Therefore, the main impact is multi-threaded operating efficiency, but not necessarily affect. What does it mean?

  We will inevitably encounter scene multitasking at work, our solution is nothing more than that several, multi-threaded, multi-process, coroutine, or a combination thereof parallel or concurrent tasks, we have only this article If you talk about the method uses a multi-threaded when, as GIL only affects multi-threading. All multitasking scenario, they can be divided into compute-intensive (CPU) and I / O-intensive area, if you are multi-tasking is I / O intensive, then congratulations, you want to use multiple threads to use, GIL have much influence on it, and if you multi-task is computationally intensive, so unfortunately, you better not use multi-threaded approach to solve, because does not work, or even because the lock overhead of acquiring and releasing increases .

  We list what compute-intensive and I / O-intensive tasks which probably include:

  • Compute-intensive: it simply is computationally intensive, high demand for CPU, math, matrix operations, search, image processing, etc.
  • I / O-intensive: Input Ouput, incorrect input and output, the data transfer back and forth, file operations, database, network I / O, etc.

  According to their specific business scene selection, to decide whether or not suitable for multi-threaded code.

Third, when GIL will hand over the right to use cpu

  1. I / O intensive operations, waiting time, will lock when the cpu idle hand GIL
  2. There is a special python counting ticks, ticks value reaches 100 when the lock is released Gil (Note: This value can be set ticks to extend or reduce the time thread to acquire the lock using the cpu Gil)

IV Summary

  • GIL is cpython the interpreter to bring a solution to the problem of memory leaks and deadlocks, not python comes.
  • For multi-tasking problem, if it is I / O intensive, multi-threaded use no problem, if it is computationally intensive, cautious.

Guess you like

Origin www.cnblogs.com/wsjgdxx/p/12044168.html
GIL