python多線程在IO密集型情況下為什麼會更快?__python

來源:互聯網
上載者:User

下面是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調度時間白白浪費,反而導致多線程比單線程耗時更久。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.