【Python解释器是单线程应用】

 【任意时刻,仅执行一个线程】

尽管Python解释器中可以运行多个线程,但是在任意给定的时刻只有一个线程会被解释器执行。

【GIL锁 保证同时只有一个线程运行】

 对Python虚拟机的访问是由全局解释器锁(GIL)控制的。这个锁就是用来保证同时只有一个线程运行的。

【Python虚拟机】

Python代码的执行是由Python虚拟机(又名解释器主循环)进行控制的。在主循环中同时只能有一个控制线程在执行。

在内存中可以有许多程序,但是在任意给定时刻只能有一个程序在运行。

在多线程环境中,Python虚拟机将按照下面所述方式执行:

1、设置GIL;

2、切换一个线程去运行;

3、执行下面操作之一:

  a.指定数量的字节码指令;

  b.线程主动让出控制权;

4、把线程设置回睡眠状态(切换出线程)。

5、解锁GIL;

6、重复上述步骤

Python解释器是单线程应用   IO 密集型  计算密集型 GIL global interpreter lock_换出

 

 

https://wiki.python.org/moin/GlobalInterpreterLock

 

 

In CPython, the global interpreter lock, or GIL, is a mutex that protects access to Python objects, preventing multiple threads from executing Python bytecodes at once. This lock is necessary mainly because CPython's memory management is not thread-safe. (However, since the GIL exists, other features have grown to depend on the guarantees that it enforces.)

CPython extensions must be GIL-aware in order to avoid defeating threads. For an explanation, see Global interpreter lock.

【阻碍多核】

The GIL is controversial because it prevents multithreaded CPython programs from taking full advantage of multiprocessor systems in certain situations. Note that potentially blocking or long-running operations, such as I/O, image processing, and NumPy number crunching, happen outside the GIL. Therefore it is only in multithreaded programs that spend a lot of time inside the GIL, interpreting CPython bytecode, that the GIL becomes a bottleneck.

However the GIL can degrade performance even when it is not a bottleneck. Summarizing those slides: The system call overhead is significant, especially on multicore hardware. Two threads calling a function may take twice as much time as a single thread calling the function twice. The GIL can cause I/O-bound threads to be scheduled ahead of CPU-bound threads. And it prevents signals from being delivered.