pypy 为什么没有去除 GIL?
Python解释器有哪些
- CPython: 官方默认版本,使用
C
语言开发,是Python
使用最广泛的解释器,有GIL
. - IPython:
IPython
是基于CPython
之上的交互式解释器,其它方面和CPython
相同. - PyPy:
PyPy
采用JIT(Just In Time)
也就是即时编译编译器,对Python
代码执行动态编译,目的是加快执行速度,有GIL
. - Jython: 运行在
Java
平台上的解释器,把Python
代码编译为Java
字节码执行,没有GIL
. - IronPython: IronPython和Jython类似,只不过IronPython是运行在微软.Net平台上的Python解释器,可以直接把Python代码编译成.Net的字节码,没有GIL.
回答:
因为有历史包袱,在保证兼容性的情况下改起来太痛苦,改的不好很可能原本单线程的性能都要下降(没错,Facebook 已经尝试改过了,性能非但没提升反而下降了)。
对此 PyPy 团队自己在文档里阐述过相关问题:https://doc.pypy.org/en/lates...
Yes, PyPy has a GIL. Removing the GIL is very hard. On top of CPython, you have two problems: (1) GC, in this case reference counting; (2) the whole Python language.
For PyPy, the hard issue is (2): by that I mean issues like what occurs if a mutable object is changed from one thread and read from another concurrently. This is a problem for any mutable type: it needs careful review and fixes (fine-grained locks, mostly) through the whole Python interpreter. It is a major effort, although not completely impossible, as Jython/IronPython showed. This includes subtle decisions about whether some effects are ok or not for the user (i.e. the Python programmer).
CPython has additionally the problem (1) of reference counting. With PyPy, this sub-problem is simpler: we need to make our GC multithread-aware. This is easier to do efficiently in PyPy than in CPython. It doesn’t solve the issue (2), though.
Note that there was work to support a Software Transactional Memory (STM) version of PyPy. This should give an alternative PyPy which works without a GIL, while at the same time continuing to give the Python programmer the complete illusion of having one. This work is currently a bit stalled because of its own technical difficulties.
注意加粗的部分,简而言之就是“难,难,难”。
不光 PyPy 了,你以为 CPython 就不想移除 GIL 吗?问题这是天坑,改起来太难了。
相比之下,社区里还有其他很多也很重要的工作要做,团队的全部精力不可能全都放在这上面。
P.S. 其实已经有一些进展了,2018 年底就已经有无 GIL 版本的 PyPy 了,但目前仍处于实验性阶段,还无法用于生产环境。
以上是 pypy 为什么没有去除 GIL? 的全部内容, 来源链接: utcz.com/p/938326.html