Python GIL ------- Global Interpreter Lock

By convention the first official document:

In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native threads from executing Python bytecodes at once. 

This lock is necessary mainly because CPython’s memory management is not thread-safe.

Since I am the general level of English, Baidu translated as follows:

In cpython, the global interpreter lock (Gil) is a mutex, which prevents multiple native threads run python bytecode.
This lock is necessary, mainly because cpython memory management is not thread-safe.
Well, to start into the topic:

GIL: global interpreter lock.

First seen in official documents,

The first point: the lock is in cpython interpreter, so do not take it for granted that a defect python language itself.

The second point: mutex, and you know, like other locks, but the lock is interpreter level to prevent multiple threads at the same time execute multiple threads, so this can be seen, but whether it is cpu Multi-core, you can only execute one thread at the same point in time. It can not take advantage of multithreaded and multicore cpu advantages, and parallel execution can not be achieved in the true sense, can do concurrent (parallel Simply put, at the same time can do multiple threads or processes are performed in real time. Concurrency : just quickly switch tasks, while performing the task may seem, before threads, processes, coroutines have introduced)

The third point: the reason, cptyhon memory management is not thread-safe. That is, we all go visit will cause thread-safe, memory data registers are shared, it will cause data disorder. There is a thread-level interpreter will open this part of the common resources occupancy

That there is a multi-threaded python with wool (sweater also became a hit)?

It depends on which you use multiple threads in there (the specific reasons can refer to processes, threads, coroutines piece):

IO-intensive : If you are more IO operation, multi-threading that comes into play.

Compute-intensive : almost 99% cpu require the participation of the then huh. To be honest not as fast single-threaded, single-threaded backup switching and less time to restore the site

That multicore, multithreaded do?

Multicore, multithreaded worse than single-core multi-threaded, multi-threaded Nucleation is a single reason, each release GIL, wake up that thread can acquire the GIL lock, can be seamlessly executed, but in multi-core, CPU0 released after GIL, on the other CPU thread will compete, but could immediately be GIL get CPU0, resulting in several other threads on the CPU will be awake waiting to be awakened after the switching time to be scheduled into the state, this will cause the thread thrashing (thrashing), resulting in lower efficiency

So far the cause and effect basically said and done that, how to take advantage of multicore it?

1. The simplest change python interpreter, because the root of the interpreter

2. not want to change the interpreter, the default installation is cpython ah. That multi-process

The reason: each process has its own independent GIL, without disturbing each other, so that you can execute in parallel in the true sense, so in python, the efficiency is better than multi-process multi-thread (only for multi-core CPU terms). Note, however, the process of switching but more resource-intensive than the thread, so you can not do a good job as more like multithreading to replace the original multi-threaded. So try it in the allocation process on multi-core cpu

 

 

Look forward to more garbage will typesetting

 

 

Guess you like

Origin www.cnblogs.com/gtsnow/p/10944515.html