理解python多線程(python多線程簡明教程)
對于python 多線程的理解,我花了很長時間,搜索的大部份文章都不夠通俗易懂。所以,這里力圖用簡單的例子,讓你對多線程有個初步的認識。
單線程
在好些年前的MS-DOS時代,操作系統(tǒng)處理問題都是單任務的,我想做聽音樂和看電影兩件事兒,那么一定要先排一下順序。
(好吧!我們不糾結在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循環(huán)來控制音樂的播放了兩次,每首音樂播放需要1秒鐘,sleep()來控制音樂播放的時長。接著我們又看了一場電影,
每一場電影需要5秒鐘,因為太好看了,所以我也通過for循環(huán)看兩遍。在整個休閑娛樂活動結束后,我通過
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()進行了傳參處理。體驗中國經(jīng)典歌曲和歐美大片文化。
運行結果:
>>> ======================== 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
多線程
科技在發(fā)展,時代在進步,我們的CPU也越來越快,CPU抱怨,P大點事兒占了我一定的時間,其實我同時干多個活都沒問題的;于是,操作系
統(tǒng)就進入了多任務時代。我們聽著音樂吃著火鍋的不在是夢想。
python提供了兩個模塊來實現(xiàn)多線程thread 和threading ,thread 有一些缺點,在threading 得到了彌補,為了不浪費你和時間,所以我們直
接學習threading 就可以了。
繼續(xù)對上面的例子進行改造,引入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)
創(chuàng)建了threads數(shù)組,創(chuàng)建線程t1,使用threading.Thread()方法,在這個方法中調用music方法target=music,args方法對music進行傳參。 把創(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兩個線程)
setDaemon()
setDaemon(True)將線程聲明為守護線程,必須在start() 方法調用之前設置,如果不設置為守護線程程序會被無限掛起。子線程啟動后,父線
程也繼續(xù)執(zhí)行下去,當父線程執(zhí)行完最后一條語句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
從執(zhí)行結果來看,子線程(muisc 、move )和主線程(print "all over %s" %ctime())都是同一時間啟動,但由于主線程執(zhí)行完結束,所以導致子線程也終止。
繼續(xù)調整程序:
...
if __name__ == '__main__':
for t in threads:
t.setDaemon(True)
t.start()
t.join()
print "all over %s" %ctime()
我們只對上面的程序加了個join()方法,用于等待線程終止。join()的作用是,在子線程完成運行之前,這個子線程的父線程將一直被阻塞。
注意: join()方法的位置是在for循環(huán)外的,也就是說必須等待for循環(huán)里的兩個進程都結束后,才去執(zhí)行主進程。
運行結果:
>>> ========================= 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
從執(zhí)行結果可看到,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)
...
執(zhí)行結果:
>>> ====================== 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.
之前講了多線程的一篇博客,感覺講的意猶未盡,其實,多線程非常有意思。因為我們在使用電腦的過程中無時無刻都在多進程和多線程。我們可以接著之前的例子繼續(xù)講。請先看我的上一篇博客。
從上面例子中發(fā)現(xiàn)線程的創(chuàng)建是頗為麻煩的,每創(chuàng)建一個線程都需要創(chuàng)建一個tx(t1、t2、...),如果創(chuàng)建的線程多時候這樣極其不方便。下面對通過例子進行繼續(xù)改進:
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))
#創(chuàng)建線程
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()
有趣的是我們又創(chuàng)建了一個player()函數(shù),這個函數(shù)用于判斷播放文件的類型。如果是mp3格式的,我們將調用music()函數(shù),如果是mp4格式的我們調用move()函數(shù)。哪果兩種格式都不是那么只能告訴用戶你所提供有文件我播放不了。
然后,我們創(chuàng)建了一個list的文件列表,注意為文件加上后綴名。然后我們用len(list) 來計算list列表有多少個文件,這是為了幫助我們確定循環(huán)次數(shù)。
接著我們通過一個for循環(huán),把list中的文件添加到線程中數(shù)組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
現(xiàn)在向list數(shù)組中添加一個文件,程序運行時會自動為其創(chuàng)建一個線程。
繼續(xù)改進例子:
通過上面的程序,我們發(fā)現(xiàn)player()用于判斷文件擴展名,然后調用music()和move() ,其實,music()和move()完整工作是相同的,我們?yōu)槭裁床蛔鲆慌_超級播放器呢,不管什么文件都可以播放。經(jīng)過改造,我的超級播放器誕生了。
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))
#創(chuàng)建線程
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()
首先創(chuàng)建字典list ,用于定義要播放的文件及時長(秒),通過字典的items()方法來循環(huán)的取file和time,取到的這兩個值用于創(chuàng)建線程。
接著創(chuàng)建super_player()函數(shù),用于接收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
創(chuàng)建自己的多線程類
#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}
#創(chuàng)建線程
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)
創(chuàng)建MyThread類,用于繼承threading.Thread類。
__init__()
使用類的初始化方法對func、args、name等參數(shù)進行初始化。
apply()
apply(func [, args [, kwargs ]]) 函數(shù)用于當函數(shù)參數(shù)已經(jīng)存在于一個元組或字典中時,間接地調用函數(shù)。args是一個包含將要提供給函數(shù)的按位置傳遞的參數(shù)的元組。如果省略了args,任何參數(shù)都不會被傳遞,kwargs是一個包含關鍵字參數(shù)的字典。
apply() 用法:
#不帶參數(shù)的方法
>>> def say():
print 'say in'
>>> apply(say)
say in
#函數(shù)只帶元組的參數(shù)
>>> def say(a,b):
print a,b
>>> apply(say,('hello','蟲師'))
hello 蟲師
#函數(shù)帶關鍵字參數(shù)
>>> 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類來創(chuàng)建線程。
運行結果:
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
相關文章
詳細介紹在pandas中創(chuàng)建category類型數(shù)據(jù)的幾種方法
這篇文章主要介紹了詳細介紹在pandas中創(chuàng)建category類型數(shù)據(jù)的幾種方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04python使用py2neo查詢Neo4j的節(jié)點、關系及路徑
本文介紹了使用Py2neo的NodeMatcher和RelationshipMatcher查詢圖中的節(jié)點和關系,以及通過執(zhí)行Cypher語句的查詢方式。感興趣的小伙伴請看下文2021-08-08Python基于scapy實現(xiàn)修改IP發(fā)送請求的方法示例
這篇文章主要介紹了Python基于scapy實現(xiàn)修改IP發(fā)送請求的方法,涉及Python網(wǎng)絡編程中使用scapy操作IP的相關實現(xiàn)技巧,需要的朋友可以參考下2017-07-07Python實現(xiàn)微信好友數(shù)據(jù)爬取及分析
這篇文章會基于Python對微信好友進行數(shù)據(jù)分析,這里選擇的維度主要有:性別、頭像、簽名、位置,主要采用圖表和詞云兩種形式來呈現(xiàn)結果,其中,對文本類信息會采用詞頻分析和情感分析兩種方法,感興趣的小伙伴可以了解一下2021-12-12