【Python】【并行计算】Python的GIL是什么鬼,多线程性能究竟如何

原文转自:http://cenalulu.github.io/python/gil-in-python/

<div class="inner-wrap" style="color:rgb(49,49,48);font-family:Georgia,Times,'Times New Roman',serif;font-size:16px;">
<div id="content" class="page-content" style="margin-left:0px;">
<blockquote style="font-style:italic;border-left:4px solid rgb(221,221,221);">
<p style="line-height:1.5;">前言:博主在刚接触Python的时候时常听到GIL这个词,并且发现这个词经常和Python无法高效的实现多线程划上等号。本着不光要知其然,还要知其所以然的研究态度,博主搜集了各方面的资料,花了一周内几个小时的闲暇时间深入理解了下GIL,并归纳成此文,也希望读者能通过次本文更好且客观的理解GIL。

并不是Python的特性,它是在实现Python解析器(CPython)时所引入的一个概念。就好比C++是一套语言(语法)标准,但是可以用不同的编译器来编译成可执行代码。有名的编译器例如GCC,INTEL
C++,Visual C++等。Python也一样,同样一段代码可以通过CPython,PyPy,Psyco等不同的Python执行环境来执行。像其中的JPython就没有GIL。然而因为CPython是大部分环境下默认的Python执行环境。所以在很多人的概念里CPython就是Python,也就想当然的把归结为Python语言的缺陷。所以这里要先明确一点:GIL并不是Python的特性,Python完全可以不依赖于GIL

为了避免误导,我们还是来看一下官方给出的解释:



<span class="kn" style="color:rgb(133,153,0);">from <span class="nn" style="color:rgb(147,161,161);">threading <span class="kn" style="color:rgb(133,0);">import <span class="n" style="color:rgb(147,161);">Thread
<span class="kn" style="color:rgb(133,0);">import <span class="nn" style="color:rgb(147,161);">time

<span class="k" style="color:rgb(133,0);">def <span class="nf" style="color:rgb(38,139,210);">mycounter<span class="p" style="color:rgb(147,161);">():
<span class="n" style="color:rgb(147,161);">i <span class="o" style="color:rgb(133,0);">= <span class="mi" style="color:rgb(42,152);">0
<span class="k" style="color:rgb(133,0);">for <span class="n" style="color:rgb(147,161);">
<span class="ow" style="color:rgb(133,0);">in <span class="nb" style="color:rgb(181,137,0);">range<span class="p" style="color:rgb(147,161);">(<span class="mi" style="color:rgb(42,152);">100000000<span class="p" style="color:rgb(147,161);">):
<span class="n" style="color:rgb(147,0);">= <span class="n" style="color:rgb(147,0);">+ <span class="mi" style="color:rgb(42,152);">1
<span class="k" style="color:rgb(133,0);">return <span class="bp" style="color:rgb(38,210);">True

<span class="k" style="color:rgb(133,210);">main<span class="p" style="color:rgb(147,161);">thread_array <span class="o" style="color:rgb(133,0);">= <span class="p" style="color:rgb(147,161);">{}
<span class="n" style="color:rgb(147,161);">start_time <span class="o" style="color:rgb(133,161);">time<span class="o" style="color:rgb(133,0);">.<span class="n" style="color:rgb(147,161);">time<span class="p" style="color:rgb(147,161);">()
<span class="k" style="color:rgb(133,161);">tid <span class="ow" style="color:rgb(133,152);">2<span class="p" style="color:rgb(147,161);">t <span class="o" style="color:rgb(133,161);">Thread<span class="p" style="color:rgb(147,161);">(<span class="n" style="color:rgb(147,161);">target<span class="o" style="color:rgb(133,0);">=<span class="n" style="color:rgb(147,161);">my_counter<span class="p" style="color:rgb(147,161);">)
<span class="n" style="color:rgb(147,161);">t<span class="o" style="color:rgb(133,161);">start<span class="p" style="color:rgb(147,161);">()
<span class="n" style="color:rgb(147,161);">join<span class="p" style="color:rgb(147,161);">()
<span class="n" style="color:rgb(147,161);">end_time <span class="o" style="color:rgb(133,0);">print<span class="p" style="color:rgb(147,161);">(<span class="s" style="color:rgb(42,152);">"Total time: {}"<span class="o" style="color:rgb(133,161);">format<span class="p" style="color:rgb(147,0);">- <span class="n" style="color:rgb(147,161);">start_time<span class="p" style="color:rgb(147,161);">))

dawei

【声明】:唐山站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。