這篇文章主要介紹了python主線程與子線程的結束順序實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
引用自 主線程退出對子線程的影響 的一段話:
對于程序來說,如果主進程在子進程還未結束時就已經退出,那么Linux內核會將子進程的父進程ID改為1(也就是init進程),當子進程結束后會由init進程來回收該子進程。
主線程退出后子線程的狀態依賴于它所在的進程,如果進程沒有退出的話子線程依然正常運轉。如果進程退出了,那么它所有的線程都會退出,所以子線程也就退出了。
主線程退出,進程等待所有子線程執行完畢后才結束
進程啟動后會默認產生一個主線程,默認情況下主線程創建的子線程都不是守護線程(setDaemon(False))。因此主線程結束后,子線程會繼續執行,進程會等待所有子線程執行完畢后才結束
所有線程共享一個終端輸出(線程所屬進程的終端)
import threadingimport timedef 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 threadingimport timedef 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 threadingimport timedef 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()
新聞熱點
疑難解答