下面是Python 2.7.9手冊中對GIL的簡單介紹:
The mechanism used by the CPython interpreter to assure that only one thread executes Python bytecode at a time. This simplifies the CPython implementation by making the object model (including critical built-in types such as dict) implicitly safe against concurrent access. Locking the entire interpreter makes it easier for the interpreter to be multi-threaded, at the expense of much of the parallelism afforded by multi-processor machines.
However, some extension modules, either standard or third-party, are designed so as to release the GIL when doing computationally-intensive tasks such as compression or hashing. Also, the GIL is always released when doing I/O.
Past efforts to create a “free-threaded” interpreter (one which locks shared data at a much finer granularity) have not been successful because performance suffered in the common single-processor case. It is believed that overcoming this performance issue would make the implementation much more complicated and therefore costlier to maintain.
個人理解: io是分為網路io和磁碟io,一般情況下,io有發送資料(output)和返回資料(input)兩個過程。比如以瀏覽器為主體,瀏覽器發送請求給伺服器(output),伺服器再將請求結果返回給瀏覽器(input)。python在io阻塞的情況下,會釋放GIL(global interpreter lock)鎖,其他線程會在當前線程等待傳回值(阻塞)的情況下繼續執行發送請求(output),第三個線程又會在第二個線程等待傳回值(阻塞)的情況下發送請求(output),即在同一時間片段,會有一個線程在等待資料,也會有一個線程在發資料。這就減少了io傳輸的時間。 但是,由於python在用cpu執行計算任務的時候,GIL鎖不會被釋放,python多線程其實還是使用的單核在進行cpu計算。一個cpu時間片只會分給一個線程,因此,cpu密集型的情況下,多線程並不會加快計算速度。 另,如果計算任務加鎖了,cpu時間片調度機制會在一個cpu時間片(python預設是處理完1000個位元組碼)結束後,去釋放GIL鎖,並查看其他線程是否可以執行,由於任務被加鎖,會在第二個cpu時間片繼續把時間片分給第一個線程,這會讓cpu調度時間白白浪費,反而導致多線程比單線程耗時更久。