python 多線程就這麼簡單

編程語言 Python 電影夢工廠 阿凡達 CPU cpp軟件架構獅 2018-12-08

單線程

在好些年前的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.

相關推薦

推薦中...