Python 學習系列 -- 多執行緒
轉載自:https://www.cnblogs.com/yeayee/p/4952022.html
多執行緒和多程序是什麼自行google補腦,廖雪峰官網也有,但是不夠簡潔,有點暈,所以就整個簡單的範例。
對於python 多執行緒的理解,我花了很長時間,搜尋的大部份文章都不夠通俗易懂。所以,這裡力圖用簡單的例子,讓你對多執行緒有個初步的認識。
單執行緒
在好些年前的MS-DOS時代,作業系統處理問題都是單任務的,我想做聽音樂和看電影兩件事兒,那麼一定要先排一下順序。
(好吧!我們不糾結在DOS時代是否有聽音樂和看影的應用。^_^)
from time import ctime,sleep def music(): for i in range(2): print "I was listening to music. %s" %ctime() sleep(1) def move(): for i in range(2): print "I was at the movies! %s" %ctime() sleep(5) if __name__ == '__main__': music() move() print "all over %s" %ctime()
我們先聽了一首音樂,通過for迴圈來控制音樂的播放了兩次,每首音樂播放需要1秒鐘,sleep()來控制音樂播放的時長。接著我們又看了一場電影,
每一場電影需要5秒鐘,因為太好看了,所以我也通過for迴圈看兩遍。在整個休閒娛樂活動結束後,我通過
print "all over %s" %ctime()
看了一下當前時間,差不多該睡覺了。
執行結果:
>>=========================== RESTART ================================ >>> I was listening to music. Thu Apr 17 10:47:08 2014 I was listening to music. Thu Apr 17 10:47:09 2014 I was at the movies! Thu Apr 17 10:47:10 2014 I was at the movies! Thu Apr 17 10:47:15 2014 all over Thu Apr 17 10:47:20 2014
其實,music()和move()更應該被看作是音樂和視訊播放器,至於要播放什麼歌曲和視訊應該由我們使用時決定。所以,我們對上面程式碼做了改造:
#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) if __name__ == '__main__': music(u'愛情買賣') move(u'阿凡達') print "all over %s" %ctime()
對music()和move()進行了傳參處理。體驗中國經典歌曲和歐美大片文化。
執行結果:
>>> ======================== RESTART ================================ >>> I was listening to 愛情買賣. Thu Apr 17 11:48:59 2014 I was listening to 愛情買賣. Thu Apr 17 11:49:00 2014 I was at the 阿凡達! Thu Apr 17 11:49:01 2014 I was at the 阿凡達! Thu Apr 17 11:49:06 2014 all over Thu Apr 17 11:49:11 2014
多執行緒
科技在發展,時代在進步,我們的CPU也越來越快,CPU抱怨,P大點事兒佔了我一定的時間,其實我同時幹多個活都沒問題的;於是,作業系統就進入了多工時代。我們聽著音樂吃著火鍋的不在是夢想。
python提供了兩個模組來實現多執行緒thread 和threading ,thread 有一些缺點,在threading 得到了彌補,為了不浪費你和時間,所以我們直接學習threading 就可以了。
繼續對上面的例子進行改造,引入threadring來同時播放音樂和視訊:
#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'阿凡達',)) threads.append(t2) if __name__ == '__main__': for t in threads: t.setDaemon(True) t.start() print "all over %s" %ctime()
import threading
首先匯入threading 模組,這是使用多執行緒的前提。
threads = []
t1 = threading.Thread(target=music,args=(u'愛情買賣',))
threads.append(t1)
建立了threads陣列,建立執行緒t1,使用threading.Thread()方法,在這個方法中呼叫music方法target=music,args方法對music進行傳參。 把建立好的執行緒t1裝到threads陣列中。
接著以同樣的方式建立執行緒t2,並把t2也裝到threads陣列。
for t in threads:
t.setDaemon(True)
t.start()
最後通過for迴圈遍歷陣列。(陣列被裝載了t1和t2兩個執行緒)
setDaemon()
setDaemon(True)將執行緒宣告為守護執行緒,必須在start() 方法呼叫之前設定,如果不設定為守護執行緒程式會被無限掛起。子執行緒啟動後,父執行緒也繼續執行下去,當父執行緒執行完最後一條語句print "all over %s" %ctime()後,沒有等待子執行緒,直接就退出了,同時子執行緒也一同結束。
start()
開始執行緒活動。
執行結果:
>>> ========================= RESTART ================================ >>> I was listening to 愛情買賣. Thu Apr 17 12:51:45 2014 I was at the 阿凡達! Thu Apr 17 12:51:45 2014 all over Thu Apr 17 12:51:45 2014
從執行結果來看,子執行緒(muisc 、move )和主執行緒(print "all over %s" %ctime())都是同一時間啟動,但由於主執行緒執行完結束,所以導致子執行緒也終止。
繼續調整程式:
... if __name__ == '__main__': for t in threads: t.setDaemon(True) t.start() t.join() print "all over %s" %ctime()
我們只對上面的程式加了個join()方法,用於等待執行緒終止。join()的作用是,在子執行緒完成執行之前,這個子執行緒的父執行緒將一直被阻塞。
注意: join()方法的位置是在for迴圈外的,也就是說必須等待for迴圈裡的兩個程序都結束後,才去執行主程序。
執行結果:
>>> ========================= RESTART ================================ >>> I was listening to 愛情買賣. Thu Apr 17 13:04:11 2014 I was at the 阿凡達! Thu Apr 17 13:04:11 2014 I was listening to 愛情買賣. Thu Apr 17 13:04:12 2014 I was at the 阿凡達! Thu Apr 17 13:04:16 2014 all over Thu Apr 17 13:04:21 2014
從執行結果可看到,music 和move 是同時啟動的。
開始時間4分11秒,直到呼叫主程序為4分22秒,總耗時為10秒。從單執行緒時減少了2秒,我們可以把music的sleep()的時間調整為4秒。
... def music(func): for i in range(2): print "I was listening to %s. %s" %(func,ctime()) sleep(4) ...
執行結果:
>>> ====================== RESTART ================================ >>> I was listening to 愛情買賣. Thu Apr 17 13:11:27 2014I was at the 阿凡達! Thu Apr 17 13:11:27 2014 I was listening to 愛情買賣. Thu Apr 17 13:11:31 2014 I was at the 阿凡達! Thu Apr 17 13:11:32 2014 all over Thu Apr 17 13:11:37 2014
子執行緒啟動11分27秒,主執行緒執行11分37秒。
雖然music每首歌曲從1秒延長到了4 ,但通多程線的方式執行指令碼,總的時間沒變化。
本文從感性上讓你快速理解python多執行緒的使用,更詳細的使用請參考其它文件或資料。
==========================================================
class threading.Thread()說明:
class threading.Thread(group=None, target=None, name=None, args=(), kwargs={})
This constructor should always be called with keyword arguments. Arguments are:
group should be None; reserved for future extension when a ThreadGroup class is implemented.
target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.
name is the thread name. By default, a unique name is constructed of the form “Thread-N” where N is a small decimal number.
args is the argument tuple for the target invocation. Defaults to ().
kwargs is a dictionary of keyword arguments for the target invocation. Defaults to {}.
If the subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread.__init__()) before doing
anything else to the thread.
是不是感覺感覺講的意猶未盡,其實,多執行緒非常有意思。因為我們在使用電腦的過程中無時無刻都在多程序和多執行緒。我們可以接著之前的例子繼續講:
從上面例子中發現執行緒的建立是頗為麻煩的,每建立一個執行緒都需要建立一個tx(t1、t2、...),如果建立的執行緒多時候這樣極其不方便。下面對通過例子進行繼續改進:
player.py
#coding=utf-8 from time import sleep, ctime import threading def muisc(func): for i in range(2): print 'Start playing: %s! %s' %(func,ctime()) sleep(2) def move(func): for i in range(2): print 'Start playing: %s! %s' %(func,ctime()) sleep(5) def player(name): r = name.split('.')[1] if r == 'mp3': muisc(name) else: if r == 'mp4': move(name) else: print 'error: The format is not recognized!' list = ['愛情買賣.mp3','阿凡達.mp4'] threads = [] files = range(len(list)) #建立執行緒 for i in files: t = threading.Thread(target=player,args=(list[i],)) threads.append(t) if __name__ == '__main__': #啟動執行緒 for i in files: threads[i].start() for i in files: threads[i].join() #主執行緒 print 'end:%s' %ctime()
有趣的是我們又建立了一個player()函式,這個函式用於判斷播放檔案的型別。如果是mp3格式的,我們將呼叫music()函式,如果是mp4格式的我們呼叫move()函式。哪果兩種格式都不是那麼只能告訴使用者你所提供有檔案我播放不了。
然後,我們建立了一個list的檔案列表,注意為檔案加上字尾名。然後我們用len(list) 來計算list列表有多少個檔案,這是為了幫助我們確定迴圈次數。
接著我們通過一個for迴圈,把list中的檔案新增到執行緒中陣列threads[]中。接著啟動threads[]執行緒組,最後列印結束時間。
split()可以將一個字串拆分成兩部分,然後取其中的一部分
>>> x = 'testing.py' >>> s = x.split('.')[1] >>> if s=='py': print s py
執行結果:
Start playing: 愛情買賣.mp3! Mon Apr 21 12:48:40 2014 Start playing: 阿凡達.mp4! Mon Apr 21 12:48:40 2014 Start playing: 愛情買賣.mp3! Mon Apr 21 12:48:42 2014 Start playing: 阿凡達.mp4! Mon Apr 21 12:48:45 2014 end:Mon Apr 21 12:48:50 2014
現在向list陣列中新增一個檔案,程式執行時會自動為其建立一個執行緒。
繼續改進例子:
通過上面的程式,我們發現player()用於判斷副檔名,然後呼叫music()和move() ,其實,music()和move()完整工作是相同的,我們為什麼不做一臺超級播放器呢,不管什麼檔案都可以播放。經過改造,我的超級播放器誕生了。
super_player.py
#coding=utf-8 from time import sleep, ctime import threading def super_player(file,time): for i in range(2): print 'Start playing: %s! %s' %(file,ctime()) sleep(time) #播放的檔案與播放時長 list = {'愛情買賣.mp3':3,'阿凡達.mp4':5,'我和你.mp3':4} threads = [] files = range(len(list)) #建立執行緒 for file,time in list.items(): t = threading.Thread(target=super_player,args=(file,time)) threads.append(t) if __name__ == '__main__': #啟動執行緒 for i in files: threads[i].start() for i in files: threads[i].join() #主執行緒 print 'end:%s' %ctime()
首先建立字典list ,用於定義要播放的檔案及時長(秒),通過字典的items()方法來迴圈的取file和time,取到的這兩個值用於建立執行緒。
接著建立super_player()函式,用於接收file和time,用於確定要播放的檔案及時長。
最後是執行緒啟動執行。執行結果:
Start playing: 愛情買賣.mp3! Fri Apr 25 09:45:09 2014 Start playing: 我和你.mp3! Fri Apr 25 09:45:09 2014 Start playing: 阿凡達.mp4! Fri Apr 25 09:45:09 2014 Start playing: 愛情買賣.mp3! Fri Apr 25 09:45:12 2014 Start playing: 我和你.mp3! Fri Apr 25 09:45:13 2014 Start playing: 阿凡達.mp4! Fri Apr 25 09:45:14 2014 end:Fri Apr 25 09:45:19 2014
建立自己的多執行緒類
#coding=utf-8 import threading from time import sleep, ctime class MyThread(threading.Thread): def __init__(self,func,args,name=''): threading.Thread.__init__(self) self.name=name self.func=func self.args=args def run(self): apply(self.func,self.args) def super_play(file,time): for i in range(2): print 'Start playing: %s! %s' %(file,ctime()) sleep(time) list = {'愛情買賣.mp3':3,'阿凡達.mp4':5} #建立執行緒 threads = [] files = range(len(list)) for k,v in list.items(): t = MyThread(super_play,(k,v),super_play.__name__) threads.append(t) if __name__ == '__main__': #啟動執行緒 for i in files: threads[i].start() for i in files: threads[i].join() #主執行緒 print 'end:%s' %ctime()
MyThread(threading.Thread)
建立MyThread類,用於繼承threading.Thread類。
__init__()
使用類的初始化方法對func、args、name等引數進行初始化。
apply()
apply(func [, args [, kwargs ]]) 函式用於當函式引數已經存在於一個元組或字典中時,間接地呼叫函式。args是一個包含將要提供給函式的按位置傳遞的引數的元組。如果省略了args,任何引數都不會被傳遞,kwargs是一個包含關鍵字引數的字典。
apply() 用法:
#不帶引數的方法 >>> def say(): print 'say in' >>> apply(say) say in #函式只帶元組的引數 >>> def say(a,b): print a,b >>> apply(say,('hello','蟲師')) hello 蟲師 #函式帶關鍵字引數 >>> def say(a=1,b=2): print a,b >>> def haha(**kw): apply(say,(),kw) >>> haha(a='a',b='b') a b
MyThread(super_play,(k,v),super_play.__name__)
由於MyThread類繼承threading.Thread類,所以,我們可以使用MyThread類來建立執行緒。
執行結果:
Start playing: 愛情買賣.mp3! Fri Apr 25 10:36:19 2014 Start playing: 阿凡達.mp4! Fri Apr 25 10:36:19 2014 Start playing: 愛情買賣.mp3! Fri Apr 25 10:36:22 2014 Start playing: 阿凡達.mp4! Fri Apr 25 10:36:24 2014 all end: Fri Apr 25 10:36:29 2014