溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python之線程

發(fā)布時(shí)間:2020-06-27 02:13:14 來(lái)源:網(wǎng)絡(luò) 閱讀:238 作者:lvsir666 欄目:編程語(yǔ)言

首先了解幾個(gè)知識(shí)點(diǎn):

一:當(dāng)一個(gè)進(jìn)程啟動(dòng)之后,會(huì)默認(rèn)產(chǎn)生一個(gè)主線程,因?yàn)榫€程是程序執(zhí)行流的最小單元,當(dāng)設(shè)置多線程時(shí),主線程會(huì)創(chuàng)建多個(gè)子線程,在python中,默認(rèn)情況下(其實(shí)就是setDaemon(False)),主線程執(zhí)行完自己的任務(wù)以后,就退出了,此時(shí)子線程會(huì)繼續(xù)執(zhí)行自己的任務(wù),直到自己的任務(wù)結(jié)束。

二:當(dāng)我們使用setDaemon(True)方法,設(shè)置子線程為守護(hù)線程時(shí),主線程一旦執(zhí)行結(jié)束,則全部線程全部被終止執(zhí)行,可能出現(xiàn)的情況就是,子線程的任務(wù)還沒(méi)有完全執(zhí)行結(jié)束,就被迫停止。

三:此時(shí)join的作用就凸顯出來(lái)了,join所完成的工作就是線程同步,即主線程任務(wù)結(jié)束之后,進(jìn)入阻塞狀態(tài),一直等待其他的子線程執(zhí)行結(jié)束之后,主線程在終止。

       join有一個(gè)timeout參數(shù):
            1:當(dāng)設(shè)置守護(hù)線程時(shí),含義是主線程對(duì)于子線程等待timeout的時(shí)間將會(huì)殺死該子線程,最后退出程序。所以說(shuō),如果有10個(gè)子線程,全部的等待時(shí)間就是每個(gè)timeout的累加和。簡(jiǎn)單的來(lái)說(shuō),就是給每個(gè)子線程一個(gè)timeout的時(shí)間,讓他去執(zhí)行,時(shí)間一到,不管任務(wù)有沒(méi)有完成,直接殺死。
            2:沒(méi)有設(shè)置守護(hù)線程時(shí),主線程將會(huì)等待timeout的累加和這樣的一段時(shí)間,時(shí)間一到,主線程結(jié)束,但是并沒(méi)有殺死子線程,子線程依然可以繼續(xù)執(zhí)行,直到子線程全部結(jié)束,程序退出。


先看個(gè)帶有參數(shù)的簡(jiǎn)單的線程例子

def func1(a,b):
    print a,b
    
    
t1 = Thread(target=func1, args=(1,2))

print "before...."
t1.start()
print t1.getName()  #查看線程的名字
print "after...."

執(zhí)行結(jié)果

before....
1Thread-1 
2after....


默認(rèn)情況下,主線程會(huì)等待子線程執(zhí)行完成才結(jié)束,此時(shí)isDaemon函數(shù)值是False,我們可已查看

def fun():

    for item in range(50):
        print item
        time.sleep(1)

        

t2 = Thread(target=fun)
print t2.isDaemon()
t2.start()
print "after"
print "after"
print "after"
print "after end"
time.sleep(10)

執(zhí)行結(jié)果,主線程會(huì)等待子線程完成才結(jié)束

False
0
 after
after
after
after end
1
2
3
4
..
..


可設(shè)置setDaemon的值為True,使得主線程不等待子線程執(zhí)行完成,主線程執(zhí)行完畢不管子線程是否執(zhí)行完畢都會(huì)被終止執(zhí)行

def fun():
    for item in range(50):
        print item
        time.sleep(1)
       

t3 = Thread(target=fun)
t3.setDaemon(True)
t3.start()

print "after"
print "after"
print "after"
print "after end"
time.sleep(5)

執(zhí)行結(jié)果,4被打印后程序就結(jié)束

0after
after
after
after end
1
2
3
4


設(shè)置setDaemon 為True時(shí),我們?cè)囋噅oin的作用

def fun():
    for item in range(50):
        print item
        time.sleep(1)

        

t4 = Thread(target=fun)
print "before"
t4.setDaemon(True)
t4.start()
t4.join(5)
print "after"
print "after"
print "after"
print "after end"

執(zhí)行結(jié)果子線程執(zhí)行5條就被終止

before
0
1
2
3
4
after
after
after
after end


不設(shè)置setDaemon時(shí),即其值為False時(shí),我們?cè)O(shè)置join看看效果

def fun():
    for item in range(50):
        print item
        time.sleep(1)

t4 = Thread(target=fun)
print "before"
#t4.setDaemon(True)
t4.start()
t4.join(5)

print "after"
print "after"
print "after"
print "after end"

執(zhí)行結(jié)果,會(huì)發(fā)現(xiàn)主線程會(huì)等待子線程完成才推出

before
0
1
2
3
4
after
after
after
after end
5
6
..
..


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI