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

python主線程與子線程的結(jié)束順序?qū)嵗馕?/h1>
 更新時(shí)間:2019年12月17日 09:11:45   作者:luozx207  
這篇文章主要介紹了python主線程與子線程的結(jié)束順序?qū)嵗馕?文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了python主線程與子線程的結(jié)束順序?qū)嵗馕?文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

引用自 主線程退出對(duì)子線程的影響 的一段話:

對(duì)于程序來說,如果主進(jìn)程在子進(jìn)程還未結(jié)束時(shí)就已經(jīng)退出,那么Linux內(nèi)核會(huì)將子進(jìn)程的父進(jìn)程ID改為1(也就是init進(jìn)程),當(dāng)子進(jìn)程結(jié)束后會(huì)由init進(jìn)程來回收該子進(jìn)程。

主線程退出后子線程的狀態(tài)依賴于它所在的進(jìn)程,如果進(jìn)程沒有退出的話子線程依然正常運(yùn)轉(zhuǎn)。如果進(jìn)程退出了,那么它所有的線程都會(huì)退出,所以子線程也就退出了。

主線程退出,進(jìn)程等待所有子線程執(zhí)行完畢后才結(jié)束

進(jìn)程啟動(dòng)后會(huì)默認(rèn)產(chǎn)生一個(gè)主線程,默認(rèn)情況下主線程創(chuàng)建的子線程都不是守護(hù)線程(setDaemon(False))。因此主線程結(jié)束后,子線程會(huì)繼續(xù)執(zhí)行,進(jìn)程會(huì)等待所有子線程執(zhí)行完畢后才結(jié)束

所有線程共享一個(gè)終端輸出(線程所屬進(jìn)程的終端)

import threading
import time
def child_thread1():
  for i in range(100):
    time.sleep(1)
    print('child_thread1_running...')
def parent_thread():
  print('parent_thread_running...')
  thread1 = threading.Thread(target=child_thread1)
  thread1.start()
  print('parent_thread_exit...')
if __name__ == "__main__":
  parent_thread()

輸出為:

parent_thread_running...
parent_thread_exit...
child_thread1_running...
child_thread1_running...
child_thread1_running...
child_thread1_running...
...

可見父線程結(jié)束后,子線程仍在運(yùn)行,此時(shí)結(jié)束進(jìn)程,子線程才會(huì)被終止

主線程結(jié)束后進(jìn)程不等待守護(hù)線程完成,立即結(jié)束

當(dāng)設(shè)置一個(gè)線程為守護(hù)線程時(shí),此線程所屬進(jìn)程不會(huì)等待此線程運(yùn)行結(jié)束,進(jìn)程將立即結(jié)束

import threading
import time
def child_thread1():
  for i in range(100):
    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)
  thread1.start()
  thread2.start()
  print('parent_thread_exit...')
if __name__ == "__main__":
  parent_thread()

輸出:

parent_thread_running...
parent_thread_exit...
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_thread2_running...child_thread1_running...

Process finished with exit code 0

thread1是守護(hù)線程,thread2非守護(hù)線程,因此,進(jìn)程會(huì)等待thread2完成后結(jié)束,而不會(huì)等待thread1完成

注意:子線程會(huì)繼承父線程中daemon的值,即守護(hù)線程開啟的子線程仍是守護(hù)線程

主線程等待子線程完成后結(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í)行

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python對(duì)象的生命周期源碼學(xué)習(xí)

    Python對(duì)象的生命周期源碼學(xué)習(xí)

    這篇文章主要為大家介紹了Python對(duì)象的生命周期源碼學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Python滲透測(cè)試入門之Scapy庫(kù)的使用詳解

    Python滲透測(cè)試入門之Scapy庫(kù)的使用詳解

    Scapy?是一個(gè)用來解析底層網(wǎng)絡(luò)數(shù)據(jù)包的Python模塊和交互式程序,該程序?qū)Φ讓影幚磉M(jìn)行了抽象打包,使得對(duì)網(wǎng)絡(luò)數(shù)據(jù)包的處理非常簡(jiǎn)便。本文就來聊聊它的具體使用,希望對(duì)大家有所幫助
    2023-03-03
  • python numpy中array與pandas的DataFrame轉(zhuǎn)換方式

    python numpy中array與pandas的DataFrame轉(zhuǎn)換方式

    這篇文章主要介紹了python numpy中array與pandas的DataFrame轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Python內(nèi)置加密模塊用法解析

    Python內(nèi)置加密模塊用法解析

    這篇文章主要介紹了Python內(nèi)置加密模塊用法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Python super()方法原理詳解

    Python super()方法原理詳解

    這篇文章主要介紹了Python super()方法原理詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • 用vscode開發(fā)python的步驟詳解

    用vscode開發(fā)python的步驟詳解

    這篇文章主要介紹了用vscode開發(fā)python的步驟詳解,本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08
  • Java Spring項(xiàng)目國(guó)際化(i18n)詳細(xì)方法與實(shí)例

    Java Spring項(xiàng)目國(guó)際化(i18n)詳細(xì)方法與實(shí)例

    這篇文章主要介紹了Java Spring項(xiàng)目國(guó)際化詳細(xì)方法與實(shí)例,需要的朋友可以參考下
    2020-03-03
  • 在Python中使用全局日志時(shí)需要注意的問題

    在Python中使用全局日志時(shí)需要注意的問題

    這篇文章主要介紹了在Python中使用全局日志時(shí)需要注意的問題, 作者由uliweb使用時(shí)遇到的問題分析全局日志出現(xiàn)錯(cuò)誤時(shí)的解決方法,需要的朋友可以參考下
    2015-05-05
  • Python中Tkinter組件Listbox的具體使用

    Python中Tkinter組件Listbox的具體使用

    本文主要介紹了Python中Tkinter組件Listbox的具體使用,Listbox組件用于顯示一個(gè)選擇列表,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Python文件及目錄處理的方法

    Python文件及目錄處理的方法

    這篇文章主要介紹了Python文件及目錄處理的方法,Python可以用于處理文本文件和二進(jìn)制文件,比如創(chuàng)建文件、讀寫文件等操作。本文介紹Python處理目錄以及文件的相關(guān)資料,需要的朋友可以參考一下
    2021-12-12

最新評(píng)論