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

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

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

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

下面僅以多線程為例:

首先需要明確幾個(gè)概念:

知識(shí)點(diǎn)一:

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

見下面 例子一。

知識(shí)點(diǎn)二:

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

見下面例子二。

知識(shí)點(diǎn)三:

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

例子見下面三。

知識(shí)點(diǎn)四:

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

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

一:Python多線程的默認(rèn)情況

import threading
import time

def run():
    time.sleep(2)
    print('當(dāng)前線程的名字是: ', 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('一共用時(shí):', time.time()-start_time)

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

關(guān)鍵:

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

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

import threading
import time

def run():

    time.sleep(2)
    print('當(dāng)前線程的名字是: ', 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('一共用時(shí):', time.time()-start_time)

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

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

關(guān)鍵點(diǎn):

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

三:join的作用

import threading
import time

def run():

    time.sleep(2)
    print('當(dāng)前線程的名字是: ', 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('一共用時(shí):', time.time()-start_time)

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

關(guān)鍵點(diǎ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()時(shí)被阻塞,等待thread2結(jié)束后才會(huì)執(zhí)行下一句

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

總結(jié)

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

相關(guān)文章

  • Python實(shí)現(xiàn)導(dǎo)彈自動(dòng)追蹤代碼實(shí)例

    Python實(shí)現(xiàn)導(dǎo)彈自動(dòng)追蹤代碼實(shí)例

    這篇文章主要介紹了Python實(shí)現(xiàn)導(dǎo)彈自動(dòng)追蹤代碼實(shí)例,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-11-11
  • 簡單了解Python write writelines區(qū)別

    簡單了解Python write writelines區(qū)別

    這篇文章主要介紹了簡單了解Python write writelines區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • python heic后綴圖片文件轉(zhuǎn)換成jpg格式的操作

    python heic后綴圖片文件轉(zhuǎn)換成jpg格式的操作

    這篇文章主要介紹了python heic后綴圖片文件轉(zhuǎn)換成jpg格式的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • Python在字典中查找元素的3種方式

    Python在字典中查找元素的3種方式

    這篇文章主要介紹了Python在字典中查找元素的3種方式,字典是另一種可變?nèi)萜髂P?且可存儲(chǔ)任意類型對象,需要的朋友可以參考下
    2023-04-04
  • django的csrf實(shí)現(xiàn)過程詳解

    django的csrf實(shí)現(xiàn)過程詳解

    這篇文章主要介紹了django的csrf實(shí)現(xiàn)過程相加,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • Python?matplotlib的spines模塊實(shí)例詳解

    Python?matplotlib的spines模塊實(shí)例詳解

    作為程序員,經(jīng)常需要進(jìn)行繪圖,下面這篇文章主要給大家介紹了關(guān)于Python?matplotlib的spines模塊的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • Python接口自動(dòng)化之淺析requests模塊get請求

    Python接口自動(dòng)化之淺析requests模塊get請求

    這篇文章主要介紹了requests模塊get請求,在Python語言中,雖然提供了urllib2和urllib的庫,但是相比較而言,Requests仍然是實(shí)現(xiàn)接口測試最好的選擇,因?yàn)樗怯闷饋砀雍啽?/div> 2021-08-08
  • 跟老齊學(xué)Python之深入變量和引用對象

    跟老齊學(xué)Python之深入變量和引用對象

    本講再次提及變量和引用對象,就是要讓看官對變量和賦值有一個(gè)知其然和知其所以然的認(rèn)識(shí)。當(dāng)然,最后能不能達(dá)到此目的,主要看我是不是說的通俗易懂了。如果您沒有明白,就說明我說的還不夠好,可以聯(lián)系我,我再為您效勞。
    2014-09-09
  • Python中parsel兩種獲取數(shù)據(jù)方式小結(jié)

    Python中parsel兩種獲取數(shù)據(jù)方式小結(jié)

    本文主要介紹了Python中parsel兩種獲取數(shù)據(jù)方式小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • python如何在終端里面顯示一張圖片

    python如何在終端里面顯示一張圖片

    這篇文章主要為大家詳細(xì)介紹了python如何在終端里面顯示一張圖片的方法,感興趣的小伙伴們可以參考一下
    2016-08-08

最新評論