溫馨提示×

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

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

python中如何實(shí)現(xiàn)多線程

發(fā)布時(shí)間:2020-11-09 09:37:11 來源:億速云 閱讀:177 作者:小新 欄目:編程語言

python中如何實(shí)現(xiàn)多線程?這個(gè)問題可能是我們?nèi)粘W(xué)習(xí)或工作經(jīng)常見到的。希望通過這個(gè)問題能讓你收獲頗深。下面是小編給大家?guī)淼膮⒖純?nèi)容,讓我們一起來看看吧!

關(guān)于多線程

python提供了兩個(gè)模塊來實(shí)現(xiàn)多線程thread 和threading ,thread 有一些缺點(diǎn),在threading 得到了彌補(bǔ),為了不浪費(fèi)你和時(shí)間,所以我們直接學(xué)習(xí)threading 就可以了。

繼續(xù)對(duì)上面的例子進(jìn)行改造,引入threadring來同時(shí)播放音樂和視頻:

#coding=utf-8
import threading
from time import ctime,sleep
def music(func):
 for i in range(2):
 print "I was listening to %s. %s" %(func,ctime())
 sleep(1)
def move(func):
 for i in range(2):
 print "I was at the %s! %s" %(func,ctime())
 sleep(5)
threads = []
t1 = threading.Thread(target=music,args=(u'愛情買賣',))
threads.append(t1)
t2 = threading.Thread(target=move,args=(u'阿凡達(dá)',))
threads.append(t2)
if __name__ == '__main__':
 for t in threads:
 t.setDaemon(True)
 t.start()
 print "all over %s" %ctime()
import threading
首先導(dǎo)入threading 模塊,這是使用多線程的前提。
threads = []
t1 = threading.Thread(target=music,args=(u'愛情買賣',))
threads.append(t1)

創(chuàng)建了threads數(shù)組,創(chuàng)建線程t1,使用threading.Thread()方法,在這個(gè)方法中調(diào)用music方法target=music,args方法對(duì)music進(jìn)行傳參。 把創(chuàng)建好的線程t1裝到threads數(shù)組中。

接著以同樣的方式創(chuàng)建線程t2,并把t2也裝到threads數(shù)組。

for t in threads:
 t.setDaemon(True)
 t.start()

最后通過for循環(huán)遍歷數(shù)組。(數(shù)組被裝載了t1和t2兩個(gè)線程)

setDaemon()

setDaemon(True)將線程聲明為守護(hù)線程,必須在start() 方法調(diào)用之前設(shè)置,如果不設(shè)置為守護(hù)線程程序會(huì)被無限掛起。子線程啟動(dòng)后,父線程也繼續(xù)執(zhí)行下去,當(dāng)父線程執(zhí)行完最后一條語句print "all over %s" %ctime()后,沒有等待子線程,直接就退出了,同時(shí)子線程也一同結(jié)束。

start()

開啟線程活動(dòng),運(yùn)行結(jié)果:

>>> ========================= RESTART ================================
>>>
I was listening to 愛情買賣. Thu Apr 17 12:51:45 2014 I was at the 阿凡達(dá)! Thu Apr 17 12:51:45 2014 all over Thu Apr 17 12:51:45 2014

從執(zhí)行結(jié)果來看,子線程(muisc 、move )和主線程(print "all over %s" %ctime())都是同一時(shí)間啟動(dòng),但由于主線程執(zhí)行完結(jié)束,所以導(dǎo)致子線程也終止。

繼續(xù)調(diào)整程序:

...
if __name__ == '__main__':
 for t in threads:
 t.setDaemon(True)
 t.start()
 
 t.join()
 print "all over %s" %ctime()

我們只對(duì)上面的程序加了個(gè)join()方法,用于等待線程終止。join()的作用是,在子線程完成運(yùn)行之前,這個(gè)子線程的父線程將一直被阻塞。

注意: join()方法的位置是在for循環(huán)外的,也就是說必須等待for循環(huán)里的兩個(gè)進(jìn)程都結(jié)束后,才去執(zhí)行主進(jìn)程。

運(yùn)行結(jié)果:

>>> ========================= RESTART ================================
>>>
I was listening to 愛情買賣. Thu Apr 17 13:04:11 2014 I was at the 阿凡達(dá)! Thu Apr 17 13:04:11 2014
I was listening to 愛情買賣. Thu Apr 17 13:04:12 2014
I was at the 阿凡達(dá)! Thu Apr 17 13:04:16 2014
all over Thu Apr 17 13:04:21 2014

從執(zhí)行結(jié)果可看到,music 和move 是同時(shí)啟動(dòng)的。

開始時(shí)間4分11秒,直到調(diào)用主進(jìn)程為4分22秒,總耗時(shí)為10秒。從單線程時(shí)減少了2秒,我們可以把music的sleep()的時(shí)間調(diào)整為4秒。

..
def music(func):
 for i in range(2):
 print "I was listening to %s. %s" %(func,ctime())
 sleep(4)

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

>>> ====================== RESTART ================================
>>>
I was listening to 愛情買賣. Thu Apr 17 13:11:27 2014I was at the 阿凡達(dá)! Thu Apr 17 13:11:27 2014
I was listening to 愛情買賣. Thu Apr 17 13:11:31 2014
I was at the 阿凡達(dá)! Thu Apr 17 13:11:32 2014
all over Thu Apr 17 13:11:37 2014

子線程啟動(dòng)11分27秒,主線程運(yùn)行11分37秒。

雖然music每首歌曲從1秒延長(zhǎng)到了4 ,但通多程線的方式運(yùn)行腳本,總的時(shí)間沒變化。

感謝各位的閱讀!看完上述內(nèi)容,你們對(duì)python中如何實(shí)現(xiàn)多線程大概了解了嗎?希望文章內(nèi)容對(duì)大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(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