推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
sy52676
V2EX  ›  Python

Python 中的 GIL 如果可以去掉的话,是不是只要有线程锁就完全不会产生负面影响?

  •  
  •   sy52676 · May 31, 2018 · 4432 views
    This topic created in 3039 days ago, the information mentioned may be changed or developed.
    菜鸟,搞不明白为什么有了线程锁还要有 GIL。是因为 GIL 是被动添加上去的吗?
    GIL 和线程锁的区别又在哪里呢?
    7 replies  •  2018-06-01 12:00:48 +08:00
    neoblackcap
        1
    neoblackcap  
       May 31, 2018   ❤️ 1
    颗粒度不一样,因为如果去掉 GIL 相当于现在的 Python 里面的每一个操作都是不安全的,哪怕你是一个简单的加法运算,因为加法运行也可能变动引用计数。天知道这里面有多少依赖 CPython 实现的。
    还有就是 GvR 要求去掉 GIL 不能减慢单线程性能,要兼容性就必须上细颗粒锁,锁多了性能肯定会下降,这才是为什么 CPython 的 GIL 不好去掉的缘故
    sy52676
        2
    sy52676  
    OP
       May 31, 2018
    @neoblackcap 好像明白一些,但是你讲的好高深。。。
    jimmyczm
        3
    jimmyczm  
       May 31, 2018
    @sy52676 简单和你说一下吧,我也不知道
    HelloAmadeus
        4
    HelloAmadeus  
       May 31, 2018 via Android
    简单说吧,引用计数不去掉,估计 gil 锁就去不掉
    qoras
        5
    qoras  
       May 31, 2018
    保证使用基础结构时不用加锁, 如 dict
    neoblackcap
        6
    neoblackcap  
       Jun 1, 2018
    @qoras 不可能,因为 CPython 里面所有的对象都对应底层 C 语言级别 PyObject 结构体,每个对象都有一个引用计数,不用 GIL 没法保证引用计数的变动线程安全。
    如果改动的话,Java 就是一个很好的例子,大锁改成细颗粒锁,外加 concurrent 包,同时啊,引用计数应该改成 tracing GC。引用计数在现在的技术下不比 tracing gc 优秀
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2373 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 10:37 · PVG 18:37 · LAX 03:37 · JFK 06:37
    ♥ Do have faith in what you're doing.