多線程和多進(jìn)程是什么自行google補(bǔ)腦 對(duì)于python 多線程的理解,我花了很長時(shí)間,搜索的大部份文章都不夠通俗易懂。所以,這里力圖用簡單的例子,讓你對(duì)多線程有個(gè)初步的認(rèn)識(shí)。
單線程
在好些年前的MS-DOS時(shí)代,操作系統(tǒng)處理問題都是單任務(wù)的,我想做聽音樂和看電影兩件事兒,那么一定要先排一下順序。 (好吧!我們不糾結(jié)在DOS時(shí)代是否有聽音樂和看影的應(yīng)用。^_^) 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循環(huán)來控制音樂的播放了兩次,每首音樂播放需要1秒鐘,sleep()來控制音樂播放的時(shí)長。接著我們又看了一場電影, 每一場電影需要5秒鐘,因?yàn)樘每戳?,所以我也通過for循環(huán)看兩遍。在整個(gè)休閑娛樂活動(dòng)結(jié)束后,我通過 print "all over %s" %ctime()
看了一下當(dāng)前時(shí)間,差不多該睡覺了。 運(yùn)行結(jié)果: >>=========================== 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
其實(shí),music()和move()更應(yīng)該被看作是音樂和視頻播放器,至于要播放什么歌曲和視頻應(yīng)該由我們使用時(shí)決定。所以,我們對(duì)上面代碼做了改造: #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'阿凡達(dá)') print "all over %s" %ctime() 對(duì)music()和move()進(jìn)行了傳參處理。體驗(yàn)中國經(jīng)典歌曲和歐美大片文化。 運(yùn)行結(jié)果: >>> ======================== 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 阿凡達(dá)! Thu Apr 17 11:49:01 2014 I was at the 阿凡達(dá)! Thu Apr 17 11:49:06 2014 all over Thu Apr 17 11:49:11 2014
多線程
科技在發(fā)展,時(shí)代在進(jìn)步,我們的CPU也越來越快,CPU抱怨,P大點(diǎn)事兒占了我一定的時(shí)間,其實(shí)我同時(shí)干多個(gè)活都沒問題的;于是,操作系統(tǒng)就進(jìn)入了多任務(wù)時(shí)代。我們聽著音樂吃著火鍋的不在是夢想。 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秒延長到了4 ,但通多程線的方式運(yùn)行腳本,總的時(shí)間沒變化。
本文從感性上讓你快速理解python多線程的使用,更詳細(xì)的使用請(qǐng)參考其它文檔或資料。 ========================================================== 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.
|
|