python線程編寫的兩種方法
Python中如果要使用線程的話,python的lib中提供了兩種方式。一種是函數(shù)式,一種是用類來(lái)包裝的線程對(duì)象。舉兩個(gè)簡(jiǎn)單的例子希望起到拋磚引玉的作用,關(guān)于多線程編程的其他知識(shí)例如互斥、信號(hào)量、臨界區(qū)等請(qǐng)參考python的文檔及相關(guān)資料。
1、調(diào)用thread模塊中的start_new_thread()函數(shù)來(lái)產(chǎn)生新的線程,請(qǐng)看代碼:
python代碼
###thread_example.py
importtime
importthread
deftimer(no,interval):#自己寫的線程函數(shù)
whileTrue:
print'Thread:(%d)Time:%s'%(no,time.ctime())
time.sleep(interval)
deftest():#使用thread.start_new_thread()來(lái)產(chǎn)生2個(gè)新的線程
thread.start_new_thread(timer,(1,1))
thread.start_new_thread(timer,(2,3))
if__name__=='__main__':
test()
這個(gè)是thread.start_new_thread(function,args[,kwargs])函數(shù)原型,其中function參數(shù)是你將要調(diào)用的線程函數(shù);args是講傳遞給你的線程函數(shù)的參數(shù),他必須是個(gè)tuple類型;而kwargs是可選的參數(shù)。
線程的結(jié)束一般依靠線程函數(shù)的自然結(jié)束;也可以在線程函數(shù)中調(diào)用thread.exit(),他拋出SystemExitexception,達(dá)到退出線程的目的。
2、通過(guò)調(diào)用threading模塊繼承threading.Thread類來(lái)包裝一個(gè)線程對(duì)象。請(qǐng)看代碼:
python代碼
importthreading
importtime
classtimer(threading.Thread):#我的timer類繼承自threading.Thread類
def__init__(self,no,interval):
#在我重寫__init__方法的時(shí)候要記得調(diào)用基類的__init__方法
threading.Thread.__init__(self)
self.no=no
self.interval=interval
defrun(self):#重寫run()方法,把自己的線程函數(shù)的代碼放到這里
whileTrue:
print'ThreadObject(%d),Time:%s'%(self.no,time.ctime())
time.sleep(self.interval)
deftest():
threadone=timer(1,1)#產(chǎn)生2個(gè)線程對(duì)象
threadtwo=timer(2,3)
threadone.start()#通過(guò)調(diào)用線程對(duì)象的.start()方法來(lái)激活線程
threadtwo.start()
if__name__=='__main__':
test()
其實(shí)thread和threading的模塊中還包含了其他的很多關(guān)于多線程編程的東西,例如鎖、定時(shí)器、獲得激活線程列表等等。
以上內(nèi)容為大家介紹了python中的反斜杠,希望對(duì)大家有所幫助,如果想要了解更多Python相關(guān)知識(shí),請(qǐng)關(guān)注IT培訓(xùn)機(jī)構(gòu):千鋒教育。http://www.mobiletrain.org/

猜你喜歡LIKE
相關(guān)推薦HOT
更多>>
如何使用Pandas處理Excel?
如何使用Pandas處理Excel?做過(guò)行政或者人事,或者對(duì)此有過(guò)了解的小伙伴,一定對(duì)下發(fā)各個(gè)部分的表有著非常深刻的印象,最常見(jiàn)的就是需要我們將一...詳情>>
2023-11-14 07:43:15
python中np.insert()函數(shù)的使用方法
python中np.insert()函數(shù)的使用方法在numpy數(shù)組操作中,np.append()方法可以在每行每列的最后添加數(shù)據(jù),但其位置是規(guī)定的,那如果想要指定添加...詳情>>
2023-11-14 05:06:13
SVM在python中的原理如何理解?
SVM在python中的原理如何理解?在python中除了編程化的知識(shí)點(diǎn)外,對(duì)于數(shù)學(xué)方法的算法也有所涉及,SVM就是一種很好地體現(xiàn)。我們學(xué)習(xí)過(guò)數(shù)學(xué)中的坐...詳情>>
2023-11-14 04:30:04
python處理絕對(duì)路徑和相對(duì)路徑函數(shù)有哪些?
python處理絕對(duì)路徑和相對(duì)路徑函數(shù)有哪些?絕對(duì)路徑和相對(duì)路徑是什么?絕對(duì)路徑:從根文件夾開(kāi)始,Windows系統(tǒng)以盤符(C:)作為根文件夾,OSX或Lin...詳情>>
2023-11-14 03:33:02熱門推薦
如何使用python any()判斷多元素?
沸如何使用Pandas處理Excel?
熱python函數(shù)中的參數(shù)有哪些?
熱python中pygal模塊如何使用?
新Python的excel處理操作
python中doctest庫(kù)是什么?
python中series是什么意思
python中np.insert()函數(shù)的使用方法
SVM在python中的原理如何理解?
Python描述符中有哪三種方法?
python處理絕對(duì)路徑和相對(duì)路徑函數(shù)有哪些?
python單繼承和多繼承如何定義?
python封裝中的私有如何理解?
python模塊引入的三種方式
技術(shù)干貨







快速通道 更多>>
-
課程介紹
點(diǎn)擊獲取大綱 -
就業(yè)前景
查看就業(yè)薪資 -
學(xué)習(xí)費(fèi)用
了解課程價(jià)格 -
優(yōu)惠活動(dòng)
領(lǐng)取優(yōu)惠券 -
學(xué)習(xí)資源
領(lǐng)3000G教程 -
師資團(tuán)隊(duì)
了解師資團(tuán)隊(duì) -
實(shí)戰(zhàn)項(xiàng)目
獲取項(xiàng)目源碼 -
開(kāi)班地區(qū)
查看來(lái)校路線