zoukankan      html  css  js  c++  java
  • Python解释器是单线程应用 IO 密集型 计算密集型 GIL global interpreter lock

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

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

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

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

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

    【Python虚拟机】

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

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

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

    1、设置GIL;

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

    3、执行下面操作之一:

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

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

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

    5、解锁GIL;

    6、重复上述步骤

    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.

     

     

     

  • 相关阅读:
    Microsoft 基准安全分析器(MBSA)
    神奇的C++模版
    Windows下的Memcache安装
    BisonFlex 笔记
    虚函数背后的秘密
    如何切换SecureCRT的帐号
    动态生成JS 实现 .NET 网站广告管理
    fatal error C1853 预编译头文件来自编译器的早期版本 解决方法
    解决 unresolved external symbol 无法解析 _send@16
    linux远程登录
  • 原文地址:https://www.cnblogs.com/rsapaper/p/7643694.html
Copyright © 2011-2022 走看看