1. 程式人生 > 程式設計 >python主執行緒與子執行緒的結束順序例項解析

python主執行緒與子執行緒的結束順序例項解析

這篇文章主要介紹了python主執行緒與子執行緒的結束順序例項解析,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

引用自 主執行緒退出對子執行緒的影響 的一段話:

對於程式來說,如果主程序在子程序還未結束時就已經退出,那麼Linux核心會將子程序的父程序ID改為1(也就是init程序),當子程序結束後會由init程序來回收該子程序。

主執行緒退出後子執行緒的狀態依賴於它所在的程序,如果程序沒有退出的話子執行緒依然正常運轉。如果程序退出了,那麼它所有的執行緒都會退出,所以子執行緒也就退出了。

主執行緒退出,程序等待所有子執行緒執行完畢後才結束


程序啟動後會預設產生一個主執行緒,預設情況下主執行緒建立的子執行緒都不是守護執行緒(setDaemon(False))。因此主執行緒結束後,子執行緒會繼續執行,程序會等待所有子執行緒執行完畢後才結束

所有執行緒共享一個終端輸出(執行緒所屬程序的終端)

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...
...

可見父執行緒結束後,子執行緒仍在執行,此時結束程序,子執行緒才會被終止

主執行緒結束後進程不等待守護執行緒完成,立即結束

當設定一個執行緒為守護執行緒時,此執行緒所屬程序不會等待此執行緒執行結束,程序將立即結束

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完成後結束,而不會等待thread1完成

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

主執行緒等待子執行緒完成後結束

線上程A中使用B.join()表示執行緒A在呼叫join()處被阻塞,且要等待執行緒B的完成才能繼續執行

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

主執行緒在執行到thread2.join()時被阻塞,等待thread2結束後才會執行下一句

1/0 會使主執行緒報錯退出,且thread1設定了daemon=True,因此主執行緒意外退出時thread1也會立即結束。thread1.join()沒有被主執行緒執行

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。