一、基礎概念
1、守護執行緒:在主線程**執行結束後,等待其它子執行緒執行結束,守護執行緒結束
2、守護程序:隨著主程序**執行結束,守護程序結束
3、主線程執行結束,它所在的程序執行結束
4、主程序**執行結束,主程序並沒結束,等待其它子程序執行結束並**資源
二、示例
#守護執行緒
import
time
from threading import
thread
deftest1():
while 1:
time.sleep(1)
print('
hello, world')
deftest2(n):
time.sleep(4)
(n)t1 = thread(target=test1)
t1.daemon = true #
設定t1為守護執行緒
t1.start()
for i in range(3):
t2 = thread(target=test2, args=(i, ))
t2.start()
print('
*************************=
') #
到這裡,主線程**執行完畢,守護執行緒沒有結束
"""結果:
*************************=
hello, world
hello, world
hello, world10
hello, world
2"""
#守護程序
from multiprocessing import
process
deftest1():
while 1:
print('
hello, world')
deftest2(n):
(n)if
__name__ == '
__main__':
p1 = process(target=test1)
p1.daemon = true #
設定p1為守護程序
p1.start()
for i in range(3):
p2 = process(target=test2, args=(i, ))
p2.start()
print('
***************
') #
主程序**執行結束,守護程序結束
"""結果:
***************01
2"""
Python 守護執行緒
python 守護執行緒 如果你想等待子執行緒完成再退出,那就什麼都不用做。或者顯示地呼叫thread.setdaemon false 設定daemon的值為false。新的子執行緒會繼承父執行緒的daemon標誌。整個python會在所有的非守護執行緒退出後才會結束,即程序中沒有非守護執行緒存在的...
Python守護執行緒簡述
thread模組不支援守護執行緒的概念,當主線程退出時,所有的子執行緒都將終止,不管它們是否仍在工作,如果你不希望發生這種行為,就要引入守護執行緒的概念。threading模組支援守護執行緒,其工作方式是 守護執行緒一般是乙個等待客戶端請求服務的伺服器。如果沒有客戶端請求,守護執行緒就是空閒的,如果...
python執行緒鎖 守護執行緒,程序鎖 守護程序
1 守護程序 1.1 什麼是守護程序?1 守護程序會在主程序 執行結束的情況下,立即結束。2 守護程序本身其實就是乙個子程序。3 主程序在其 結束後已經執行完畢 守護程序在此時就被 然後主程序會一直等非守護的子程序都執行完畢後 子程序的資源才會結束。1.2 為什麼要用守護程序?1 守護程序本身就是乙...