python GIL--全局解释锁

python GIL--全局解释锁,第1张

概述python GIL--全局解释

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. (However,since the GIL exists,other features have grown to depend on the guarantees that it enforces.)

GIL: 全局解释

cpython解释器:

    python在执行的时候同一时刻只允许一个线程运行

因为存在GIL,所以cpython中无法利用多核,解决方法是多进程+协程

cpu任务:    

    IO密集型    

    计算密集型

    time.sleep()    等同于IO *** 作

对于IO密集型的任务:python的多线程的是有意义的

对于计算密集型的任务:python的多线程不建议使用,python不适用。

多进程+协程实际解决是IO密集型


这篇文章透彻的剖析了GIL对python多线程的影响,强烈推荐看一下:http://www.dabeaz.com/python/UnderstandingGIL.pdf 


总结

以上是内存溢出为你收集整理的python GIL--全局解释锁全部内容,希望文章能够帮你解决python GIL--全局解释锁所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1198274.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存