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

GIL對I/O綁定多線程程序的性能影響不大,因為線程在等待I/O時共享鎖。
GIL對計算型綁定多線程程序有影響,例如: 使用線程處理部分圖像的程序,不僅會因鎖定而成為單線程,而且還會看到執(zhí)行時間的增加,這種增加是由鎖的獲取和釋放開銷的結果。
2、實例
順序執(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多線程的應用,希望能對大家有所幫助,更多知識盡在python學習網(wǎng)。
當前標題:創(chuàng)新互聯(lián)Python教程:GIL在Python多線程的應用
瀏覽路徑:http://www.dlmjj.cn/article/dheehoc.html


咨詢
建站咨詢
