欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

多線程python的實現(xiàn)及多線程有序性

 更新時間:2022年06月30日 14:54:31   作者:Efred.D  
這篇文章主要介紹了多線程python的實現(xiàn)及多線程有序性,多線程一般用于同時調(diào)用多個函數(shù),cpu時間片輪流分配給多個任務

前言

多線程一般用于同時調(diào)用多個函數(shù),cpu時間片輪流分配給多個任務。 優(yōu)點是提高cpu的使用率,使計算機減少處理多個任務的總時間;缺點是如果有全局變量,調(diào)用多個函數(shù)會使全局變量被多個函數(shù)修改,造成計算錯誤,這使需要使用join方法或者設置局部變量來解決問題。python使用threading模塊來實現(xiàn)多線程,threading.join()方法是保證調(diào)用join的子線程完成后,才會分配cpu給其他的子線程,從而保證線程運行的有序性。

一、多線程運行無序問題

我們首先創(chuàng)建三個實例,t1,t2,t3 t1實例調(diào)用function1函數(shù),t2和t3函數(shù)調(diào)用function11函數(shù),他們都是對全局變量l1進行操作

代碼如下:

import threading,time
l1 = []
#創(chuàng)建RLock鎖,acquire幾次,release幾次
lock = threading.RLock()
def function1(x,y):
    for i in range(x):
        l1.append(i)
        if i == 0:
            time.sleep(1)
    end_time = time.time()
    print("t{} is finished in {}s".format(y,end_time -time1 ))
def function11(x,y):
    for i in range(x):
        l1.append(i)
    end_time = time.time()
    print("t{} is finished in {}s".format(y, end_time -time1))
#2.創(chuàng)建子線程:thread類
if __name__ == '__main__':
    t1 = threading.Thread(target= function1, args = (100,1))
    t2 = threading.Thread(target= function11, args = (100,2))
    t3 = threading.Thread(target= function11, args = (100,3))
    time1 = time.time()
    print("time starts in {}".format(time1))
    t1.start()
    t2.start()
    t3.start()
    print(l1)

結(jié)果如下:

runfile('E:/桌面/temp.py', wdir='E:/桌面')
time starts in 1656474963.9487
t2 is finished in 0.0s
t3 is finished in 0.0s
[0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
t1 is finished in 1.0152690410614014s

我們可以看到,全局變量中開頭有兩個0,而不是按著0,1,2,3的方式按序填充,所以可以得知全局變量在多線程中是被多個函數(shù)無序調(diào)用的。為了保證多線程有序調(diào)用全局變量,我們可以利用threading.join()的方法。

二、“join方法”解決多線程運行無序問題

我們重寫了function1函數(shù),并命名為function2,t1調(diào)用function2函數(shù)。t2,t3不變。

代碼如下:

import threading,time
l1 = []
#創(chuàng)建RLock鎖,acquire幾次,release幾次
lock = threading.RLock()
def function1(x,y):
    for i in range(x):
        l1.append(i)
        if i == 0:
            time.sleep(1)
    end_time = time.time()
    print("t{} is finished in {}s".format(y,end_time -time1))
def function11(x,y):
    for i in range(x):
        l1.append(i)
    end_time = time.time()
    print("t{} is finished in {}s".format(y,end_time -time1))
def function2(x,y):
    for i in range(x):
        l1.append(i)
        if i == 0:
            time.sleep(1)
    end_time = time.time()
    print("t{} is finished in {}s".format(y,end_time -time1))
#2.創(chuàng)建子線程:thread類
if __name__ == '__main__':
    t1 = threading.Thread(target= function2, args = (100,1))
    t2 = threading.Thread(target= function11, args = (100,2))
    t3 = threading.Thread(target= function11, args = (100,3))
    time1 = time.time()
    print("time starts in {}".format(time1))
    t1.start()
    t1.join()
    t2.start()
    t3.start()
    print(l1)

結(jié)果如下:

runfile('E:/桌面/temp.py', wdir='E:/桌面')
time starts in 1656476057.441827
t1 is finished in 1.0155227184295654s
t2 is finished in 1.0155227184295654s
t3 is finished in 1.0155227184295654s
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

由此可見,threading.join()方法可以解決多線程無序問題

三、threading.Thread()的常用參數(shù)

1.group:默認值None,為了實現(xiàn)ThreadGroup類而保留
2.target:在start方法中調(diào)用的可調(diào)用對象,即需要開啟線程的可調(diào)用對象,比如函數(shù)、方法
3.name:默認為“Thread-N”,字符串形式的線程名稱
4.args:默認為空元組,參數(shù)target中傳入的可調(diào)用對象的參數(shù)元組
5.kwargs:默認為空字典{},參數(shù)target中傳入的可調(diào)用對象的關鍵字參數(shù)字典
6.daemon:默認為None

總結(jié)

到此這篇關于多線程python的實現(xiàn)及多線程有序性的文章就介紹到這了,更多相關python多線程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • TensorFlow人工智能學習按索引取數(shù)據(jù)及維度變換詳解

    TensorFlow人工智能學習按索引取數(shù)據(jù)及維度變換詳解

    這篇文章主要為大家介紹了TensorFlow人工智能學習按索引取數(shù)據(jù)及維度變換的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-11-11
  • Python線程下使用鎖的技巧分享

    Python線程下使用鎖的技巧分享

    本篇文章給大家分享了Python線程下使用鎖需要注意的地方,有興趣的朋友們可以學習參考下。
    2018-09-09
  • Python爬蟲urllib和requests的區(qū)別詳解

    Python爬蟲urllib和requests的區(qū)別詳解

    這篇文章主要介紹了Python爬蟲urllib和requests的區(qū)別詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • python實現(xiàn)ftp文件傳輸系統(tǒng)(案例分析)

    python實現(xiàn)ftp文件傳輸系統(tǒng)(案例分析)

    最近做了一個簡單的文件傳輸系統(tǒng),基于ftp協(xié)議,使用python語言開發(fā),雖然python里面已經(jīng)有ftplib模塊,可以很容易的實現(xiàn)ftp服務器,這篇文章主要介紹了python實現(xiàn)ftp文件傳輸系統(tǒng)的案例分析,需要的朋友可以參考下
    2020-03-03
  • Python機器學習之實現(xiàn)模糊照片人臉恢復清晰

    Python機器學習之實現(xiàn)模糊照片人臉恢復清晰

    GFPGAN是騰訊開源的人臉修復算法,它利用預先訓練好的面部?GAN(如?StyleGAN2)中封裝的豐富和多樣的先驗因素進行盲臉?(blind?face)修復。這篇文章主要為大家介紹通過GFPGAN實現(xiàn)模糊照片人臉恢復清晰,需要的朋友可以參考一下
    2021-12-12
  • python數(shù)據(jù)類型相關知識擴展

    python數(shù)據(jù)類型相關知識擴展

    今天帶大家學習Python數(shù)據(jù)類型的擴展知識,文中有非常詳細的介紹介代碼示例,對正在學習python的小伙伴有很大的幫助,需要的朋友可以參考下
    2021-05-05
  • Python中pandas模塊DataFrame創(chuàng)建方法示例

    Python中pandas模塊DataFrame創(chuàng)建方法示例

    這篇文章主要介紹了Python中pandas模塊DataFrame創(chuàng)建方法,結(jié)合實例形式分析了DataFrame的功能,以及pandas模塊基于列表、字段與數(shù)組創(chuàng)建DataFrame的相關操作技巧,需要的朋友可以參考下
    2018-06-06
  • Python3中exp()函數(shù)用法分析

    Python3中exp()函數(shù)用法分析

    在本篇文章里小編給大家整理了關于Python3中exp()函數(shù)用法以及相關知識點,需要的朋友們學習下。
    2019-02-02
  • Python flask與fastapi性能測試方法介紹

    Python flask與fastapi性能測試方法介紹

    這篇文章主要介紹了Python flask與fastapi性能測試方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2022-12-12
  • Pyscript使用本地Pyodide配置步驟

    Pyscript使用本地Pyodide配置步驟

    PyScript是“一個用于在 HTML(如 PHP)中交錯 Python 的系統(tǒng),這篇文章主要介紹了Pyscript使用本地Pyodide配置方法,需要的朋友可以參考下
    2022-12-12

最新評論