最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python多線程以及多線程中join()的使用方法示例

 更新時間:2021年07月05日 15:17:17   作者:waws520  
join()是線程類Thread的方法,官方的說明是:等待這個線程結(jié)束,也就是說當前線程等待這個線程結(jié)束后再繼續(xù)執(zhí)行,這篇文章主要給大家介紹了關(guān)于Python多線程以及多線程中join()使用的相關(guān)資料,需要的朋友可以參考下

Python多線程與多進程中join()方法的效果是相同的。

下面僅以多線程為例:

首先需要明確幾個概念:

知識點一:

當一個進程啟動之后,會默認產(chǎn)生一個主線程,因為線程是程序執(zhí)行流的最小單元,當設(shè)置多線程時,主線程會創(chuàng)建多個子線程,在python中,默認情況下(其實就是setDaemon(False)),主線程執(zhí)行完自己的任務(wù)以后,就退出了,此時子線程會繼續(xù)執(zhí)行自己的任務(wù),直到自己的任務(wù)結(jié)束,

見下面 例子一。

知識點二:

當我們使用setDaemon(True)方法,設(shè)置子線程為守護線程時,主線程一旦執(zhí)行結(jié)束,則全部線程全部被終止執(zhí)行,可能出現(xiàn)的情況就是,子線程的任務(wù)還沒有完全執(zhí)行結(jié)束,就被迫停止,

見下面例子二。

知識點三:

此時join的作用就凸顯出來了,join所完成的工作就是線程同步,即主線程任務(wù)在設(shè)置join函數(shù)的地方,進入阻塞狀態(tài),一直等待其他的子線程執(zhí)行結(jié)束之后,主線程再開始執(zhí)行直到終止終止,

例子見下面三。

知識點四:

join有一個timeout參數(shù):

  • 當有設(shè)置守護線程時,含義是主線程對于子線程等待timeout的時間將會殺死該子線程,最后退出程序。所以說,如果有10個子線程,全部的等待時間就是每個timeout的累加和。簡單的來說,就是給每個子線程一個timeout的時間,讓他去執(zhí)行,時間一到,不管任務(wù)有沒有完成,直接殺死。
  • 沒有設(shè)置守護線程時,主線程將會等待timeout的累加和這樣的一段時間,時間一到,主線程結(jié)束,但是并沒有殺死子線程,子線程依然可以繼續(xù)執(zhí)行,直到子線程全部結(jié)束,程序退出。

一:Python多線程的默認情況

import threading
import time

def run():
    time.sleep(2)
    print('當前線程的名字是: ', threading.current_thread().name)
    time.sleep(2)


if __name__ == '__main__':

    start_time = time.time()

    print('這是主線程:', threading.current_thread().name)
    thread_list = []
    for i in range(5):
        t = threading.Thread(target=run)
        thread_list.append(t)

    for t in thread_list:
        t.start()

    print('主線程結(jié)束!' , threading.current_thread().name)
    print('一共用時:', time.time()-start_time)

其執(zhí)行結(jié)果如下:

關(guān)鍵:

  • 計時是對主線程計時,主線程結(jié)束,計時隨之結(jié)束,打印出主線程的用時。
  • 主線程的任務(wù)完成之后,主線程隨之結(jié)束,子線程繼續(xù)執(zhí)行自己的任務(wù),直到全部的子線程的任務(wù)全部結(jié)束,程序結(jié)束。

二:設(shè)置守護線程

import threading
import time

def run():

    time.sleep(2)
    print('當前線程的名字是: ', threading.current_thread().name)
    time.sleep(2)


if __name__ == '__main__':

    start_time = time.time()

    print('這是主線程:', threading.current_thread().name)
    thread_list = []
    for i in range(5):
        t = threading.Thread(target=run)
        thread_list.append(t)

    for t in thread_list:
        t.setDaemon(True)
        t.start()

    print('主線程結(jié)束了!' , threading.current_thread().name)
    print('一共用時:', time.time()-start_time)

注意:注意請確保setDaemon()在start()之前

其執(zhí)行結(jié)果如下:

關(guān)鍵點:

非常明顯的看到,主線程結(jié)束以后,子線程還沒有來得及執(zhí)行,整個程序就退出了。

三:join的作用

import threading
import time

def run():

    time.sleep(2)
    print('當前線程的名字是: ', threading.current_thread().name)
    time.sleep(2)


if __name__ == '__main__':

    start_time = time.time()

    print('這是主線程:', threading.current_thread().name)
    thread_list = []
    for i in range(5):
        t = threading.Thread(target=run)
        thread_list.append(t)

    for t in thread_list:
        t.setDaemon(True)
        t.start()

    for t in thread_list:
        t.join()

    print('主線程結(jié)束了!' , threading.current_thread().name)
    print('一共用時:', time.time()-start_time)

其執(zhí)行結(jié)果如下:

關(guān)鍵點:

可以看到,主線程一直等待全部的子線程結(jié)束之后,主線程自身才結(jié)束,程序退出。

主程序意外退出的情況

在線程A中使用B.join()表示線程A在調(diào)用join()處被阻塞,且要等待線程B的完成才能繼續(xù)執(zhí)行

import threading
import time


def child_thread1():
    for i in range(10):
        time.sleep(1)
        print('child_thread1_running...')


def child_thread2():
    for i in range(5):
        time.sleep(1)
        print('child_thread2_running...')


def parent_thread():
    print('parent_thread_running...')
    thread1 = threading.Thread(target=child_thread1)
    thread2 = threading.Thread(target=child_thread2)
    thread1.setDaemon(True)
    thread2.setDaemon(True)
    thread1.start()
    thread2.start()
    thread2.join()
    1/0
    thread1.join()
    print('parent_thread_exit...')


if __name__ == "__main__":
    parent_thread()

輸出:

parent_thread_running...
child_thread1_running...
child_thread2_running...
child_thread1_running...
child_thread2_running...
child_thread1_running...
child_thread2_running...
child_thread1_running...
child_thread2_running...
child_thread1_running...
child_thread2_running...
Traceback (most recent call last):
  File "E:/test_thread.py", line 31, in <module>
    parent_thread()
  File "E:/test_thread.py", line 25, in parent_thread
    1/0
ZeroDivisionError: integer division or modulo by zero

主線程在執(zhí)行到thread2.join()時被阻塞,等待thread2結(jié)束后才會執(zhí)行下一句

1/0會使主線程報錯退出,且thread1設(shè)置了daemon=True,因此主線程意外退出時thread1也會立即結(jié)束。thread1.join()沒有被主線程執(zhí)行

總結(jié)

到此這篇關(guān)于Python多線程以及多線程中join()使用的文章就介紹到這了,更多相關(guān)Python多線程join()的用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解Flask框架中Flask-Login模塊的使用

    詳解Flask框架中Flask-Login模塊的使用

    Flask-Login 是一個 Flask 模塊,可以為 Flask 應(yīng)用程序提供用戶登錄功能。這篇文章將通過一些示例為大家介紹一下Flask-Login模塊的使用,需要的可以參考一下
    2023-01-01
  • Python執(zhí)行JS的四種方法

    Python執(zhí)行JS的四種方法

    本文將詳細介紹Python中執(zhí)行和調(diào)用JavaScript的多種方法,包括內(nèi)置的execjs庫、外部庫如PyExecJS、使用瀏覽器引擎和與Node.js的交互,感興趣的可以了解一下
    2023-11-11
  • python中ImageTk.PhotoImage()不顯示圖片卻不報錯問題解決

    python中ImageTk.PhotoImage()不顯示圖片卻不報錯問題解決

    這篇文章主要給大家介紹了關(guān)于在python中ImageTk.PhotoImage()不顯示圖片卻不報錯問題的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • python如何讀取.mtx文件

    python如何讀取.mtx文件

    這篇文章主要介紹了python讀取.mtx文件的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • Python獲取江蘇疫情實時數(shù)據(jù)及爬蟲分析

    Python獲取江蘇疫情實時數(shù)據(jù)及爬蟲分析

    為了關(guān)注疫情狀況,今天我們用python來爬一爬疫情的實時數(shù)據(jù),本文通過實例圖文相結(jié)合給大家介紹的非常詳細,需要的朋友參考下吧
    2021-08-08
  • Python中運維神器Psutil的用法詳解

    Python中運維神器Psutil的用法詳解

    Python的開源庫psutil為我們提供了一個強大的工具,能夠輕松獲取和分析系統(tǒng)利用率的信息,下面就跟隨小編一起深入了解一下它的具體使用吧
    2025-02-02
  • python讀取指定字節(jié)長度的文本方法

    python讀取指定字節(jié)長度的文本方法

    今天小編就為大家分享一篇python讀取指定字節(jié)長度的文本方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • 使用python快速在局域網(wǎng)內(nèi)搭建http傳輸文件服務(wù)的方法

    使用python快速在局域網(wǎng)內(nèi)搭建http傳輸文件服務(wù)的方法

    這篇文章主要介紹了使用 python快速在局域網(wǎng)內(nèi)搭建http傳輸文件服務(wù),但是這種方法不要傳輸機密文件,安全性不高,只用到http協(xié)議沒有使用任何加密協(xié)議,具體實現(xiàn)方法跟隨小編一起看看吧
    2019-11-11
  • python自動化測試三部曲之request+django實現(xiàn)接口測試

    python自動化測試三部曲之request+django實現(xiàn)接口測試

    這篇文章主要介紹了python自動化測試三部曲之request+django實現(xiàn)接口測試,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • python操作RabbitMq的三種工作模式

    python操作RabbitMq的三種工作模式

    這篇文章主要為大家介紹了python操作RabbitMq的三種工作模式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪
    2022-04-04

最新評論

佛山市| 辽宁省| 象山县| 汉川市| 五指山市| 伊通| 政和县| 临泉县| 阿拉善右旗| 绥棱县| 衡东县| 大关县| 高州市| 惠安县| 二连浩特市| 宁明县| 罗甸县| 电白县| 鄂伦春自治旗| 林甸县| 微博| 遂溪县| 克山县| 阿瓦提县| 伊金霍洛旗| 万荣县| 丹凤县| 罗山县| 新乐市| 鄂州市| 仙游县| 绥中县| 普宁市| 万年县| 昌都县| 汾阳市| 莱芜市| 马龙县| 佛学| 抚远县| 平度市|