新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:GIL在Python多線程的應(yīng)用
1、說明

GIL對I/O綁定多線程程序的性能影響不大,因?yàn)榫€程在等待I/O時(shí)共享鎖。
GIL對計(jì)算型綁定多線程程序有影響,例如: 使用線程處理部分圖像的程序,不僅會(huì)因鎖定而成為單線程,而且還會(huì)看到執(zhí)行時(shí)間的增加,這種增加是由鎖的獲取和釋放開銷的結(jié)果。
2、實(shí)例
順序執(zhí)行單線程(single_thread.py)
import threading
import time
def test_counter():
i = 0
for _ in range(100000000):
i += 1
return True
def main():
start_time = time.time()
for tid in range(2):
t1 = threading.Thread(target=test_counter)
t1.start()
t1.join()
end_time = time.time()
print("Total time:{}".format(end_time-start_time))
if __name__ == "__main__":
main()
以上就是GIL在python多線程的應(yīng)用,希望能對大家有所幫助,更多知識盡在python學(xué)習(xí)網(wǎng)。
網(wǎng)頁題目:創(chuàng)新互聯(lián)Python教程:GIL在Python多線程的應(yīng)用
URL鏈接:http://www.5511xx.com/article/dheehoc.html


咨詢
建站咨詢
