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

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

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

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

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

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

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

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

所有線程共享一個終端輸出(線程所屬進(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é)束后,子線程仍在運行,此時結(jié)束進(jìn)程,子線程才會被終止

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

當(dāng)設(shè)置一個線程為守護(hù)線程時,此線程所屬進(jì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)程會等待thread2完成后結(jié)束,而不會等待thread1完成

注意:子線程會繼承父線程中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()時被阻塞,等待thread2結(jié)束后才會執(zhí)行下一句

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

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

相關(guān)文章

  • python MySQLdb Windows下安裝教程及問題解決方法

    python MySQLdb Windows下安裝教程及問題解決方法

    這篇文章主要介紹了python MySQLdb Windows下安裝教程及問題解決方法,本文講解了安裝數(shù)據(jù)庫mysql、安裝MySQLdb等步驟,需要的朋友可以參考下
    2015-05-05
  • Python中的迭代器與生成器使用及說明

    Python中的迭代器與生成器使用及說明

    這篇文章主要介紹了Python中的迭代器與生成器使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • python去掉空格的一些常用方式

    python去掉空格的一些常用方式

    處理字符串時經(jīng)常要定制化去掉無用的空格,python 中要么用存在的常規(guī)方法,或者用正則處理,下面這篇文章主要給大家介紹了python去掉空格的一些常用方式,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-01-01
  • 使用python找出list列表中相同元素(指定元素)的所有索引

    使用python找出list列表中相同元素(指定元素)的所有索引

    這篇文章主要給大家介紹了關(guān)于使用python找出list列表中相同元素(指定元素)的所有索引,在平時開發(fā)過程中經(jīng)常遇到需要在數(shù)據(jù)中獲取特定的元素索引的信息,需要的朋友可以參考下
    2023-08-08
  • 解決python多行注釋引發(fā)縮進(jìn)錯誤的問題

    解決python多行注釋引發(fā)縮進(jìn)錯誤的問題

    今天小編就為大家分享一篇解決python多行注釋引發(fā)縮進(jìn)錯誤的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • 微信小程序python用戶認(rèn)證的實現(xiàn)

    微信小程序python用戶認(rèn)證的實現(xiàn)

    這篇文章主要介紹了微信小程序python用戶認(rèn)證的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • pyecharts在數(shù)據(jù)可視化中的應(yīng)用詳解

    pyecharts在數(shù)據(jù)可視化中的應(yīng)用詳解

    這篇文章主要介紹了pyecharts在數(shù)據(jù)可視化中的應(yīng)用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Python入門之使用pandas分析excel數(shù)據(jù)

    Python入門之使用pandas分析excel數(shù)據(jù)

    這篇文章主要給大家介紹了關(guān)于Python入門學(xué)習(xí)之使用pandas分析excel數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • pandas 使用insert插入一列

    pandas 使用insert插入一列

    這篇文章主要介紹了pandas 使用insert插入一列的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • python實操案例練習(xí)(八)

    python實操案例練習(xí)(八)

    這篇文章主要介紹了python實操案例練習(xí),本篇文章主要分享的案例內(nèi)容有記錄用戶登錄日志、模擬淘寶客服自動回復(fù),下面詳細(xì)的內(nèi)容,需要的小伙伴可以參考一下。希望對你有所幫助
    2022-02-02

最新評論