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

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

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

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

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

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

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

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

所有線程共享一個終端輸出(線程所屬進程的終端)

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é)束進程,子線程才會被終止

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

當設置一個線程為守護線程時,此線程所屬進程不會等待此線程運行結(jié)束,進程將立即結(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是守護線程,thread2非守護線程,因此,進程會等待thread2完成后結(jié)束,而不會等待thread1完成

注意:子線程會繼承父線程中daemon的值,即守護線程開啟的子線程仍是守護線程

主線程等待子線程完成后結(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設置了daemon=True,因此主線程意外退出時thread1也會立即結(jié)束。thread1.join()沒有被主線程執(zhí)行

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

相關文章

  • 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去掉空格的一些常用方式,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-01-01
  • 使用python找出list列表中相同元素(指定元素)的所有索引

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

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

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

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

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

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

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

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

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

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

    pandas 使用insert插入一列

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

    python實操案例練習(八)

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

最新評論