加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python GIL——全局解释锁

发布时间:2020-12-17 17:05:40 所属栏目:Python 来源:网络整理
导读: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 t

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?


(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读