Python實(shí)現(xiàn)多線程的兩種方式分析
本文實(shí)例講述了Python實(shí)現(xiàn)多線程的兩種方式。分享給大家供大家參考,具體如下:
目前python 提供了幾種多線程實(shí)現(xiàn)方式 thread,threading,multithreading ,其中thread模塊比較底層,而threading模塊是對(duì)thread做了一些包裝,可以更加方便的被使用。
2.7版本之前python對(duì)線程的支持還不夠完善,不能利用多核CPU,但是2.7版本的python中已經(jīng)考慮改進(jìn)這點(diǎn),出現(xiàn)了multithreading 模塊。threading模塊里面主要是對(duì)一些線程的操作對(duì)象化,創(chuàng)建Thread的class。一般來(lái)說(shuō),使用線程有兩種模式:
- A 創(chuàng)建線程要執(zhí)行的函數(shù),把這個(gè)函數(shù)傳遞進(jìn)Thread對(duì)象里,讓它來(lái)執(zhí)行;
- B 繼承Thread類,創(chuàng)建一個(gè)新的class,將要執(zhí)行的代碼 寫到run函數(shù)里面。
本文介紹兩種實(shí)現(xiàn)方法。
第一種 創(chuàng)建函數(shù)并且傳入Thread 對(duì)象中
t.py 腳本內(nèi)容
import threading,time from time import sleep, ctime def now() : return str( time.strftime( '%Y-%m-%d %H:%M:%S' , time.localtime() ) ) def test(nloop, nsec): print 'start loop', nloop, 'at:', now() sleep(nsec) print 'loop', nloop, 'done at:', now() def main(): print 'starting at:',now() threadpool=[] for i in xrange(10): th = threading.Thread(target= test,args= (i,2)) threadpool.append(th) for th in threadpool: th.start() for th in threadpool : threading.Thread.join( th ) print 'all Done at:', now() if __name__ == '__main__': main()
執(zhí)行結(jié)果:
starting at: 2018-08-29 09:34:16
start loop 0 at: 2018-08-29 09:34:16
start loop 1 at: 2018-08-29 09:34:16
start loop 2 at: 2018-08-29 09:34:16start loop 3 at: 2018-08-29 09:34:16start loop 4 start loopat: 52018-08-29 09:34:16
at: 2018-08-29 09:34:16
start loop 6 at: start loop2018-08-29 09:34:16
7 at: 2018-08-29 09:34:16
start loop 8 start loopat: 9 2018-08-29 09:34:16at:
2018-08-29 09:34:16
looploop 1 done at: 2018-08-29 09:34:18
0 done at: 2018-08-29 09:34:18
loop 3 done at: 2018-08-29 09:34:18
looploop 5 done at: 2018-08-29 09:34:18
2 done at: 2018-08-29 09:34:18
loop 4 done at: 2018-08-29 09:34:18
looploop 7 done at: 2018-08-29 09:34:18
6 done at: 2018-08-29 09:34:18
looploop 98 done at:done at: 2018-08-29 09:34:182018-08-29 09:34:18all Done at: 2018-08-29 09:34:18
第二種 創(chuàng)建繼承自Thread的類
thclass.py 腳本內(nèi)容:
import threading , time from time import sleep, ctime def now() : return str(time.strftime('%Y-%m-%d %H:%M:%S' , time.localtime())) class myThread (threading.Thread) : """docstring for myThread""" def __init__(self, nloop, nsec) : super(myThread, self).__init__() self.nloop = nloop self.nsec = nsec def run(self): print 'start loop', self.nloop, 'at:', ctime() sleep(self.nsec) print 'loop', self.nloop, 'done at:', ctime() def main(): thpool = [] print 'starting at:', now() for i in xrange(10): thpool.append(myThread(i, 2)) for th in thpool: th.start() for th in thpool: th.join() print 'all Done at:', now() if __name__ == '__main__': main()
執(zhí)行結(jié)果:
starting at: 2018-08-29 09:36:02
start loop 0 at: Wed Aug 29 09:36:02 2018
start loop 1 at: Wed Aug 29 09:36:02 2018
start loop 2 at: Wed Aug 29 09:36:02 2018
start loop 3 at: Wed Aug 29 09:36:02 2018
start loop 4 at: Wed Aug 29 09:36:02 2018
start loop 5 at: Wed Aug 29 09:36:02 2018
start loop 6 at: Wed Aug 29 09:36:02 2018
start loop 7 at: Wed Aug 29 09:36:02 2018
start loop 8 at: Wed Aug 29 09:36:02 2018
start loop 9 at: Wed Aug 29 09:36:02 2018
loop 0 done at: Wed Aug 29 09:36:04 2018
loop 1 done at: Wed Aug 29 09:36:04 2018
looploop 23 done at:done at: Wed Aug 29 09:36:04 2018Wed Aug 29 09:36:04 2018loop 4 done at: Wed Aug 29 09:36:04 2018
loop 5 done at: Wed Aug 29 09:36:04 2018
loop 6 done at: Wed Aug 29 09:36:04 2018
looploop loop 8 9 7 done at: done at: done at: Wed Aug 29 09:36:04 2018 Wed Aug 29 09:36:04 2018
Wed Aug 29 09:36:04 2018all Done at: 2018-08-29 09:36:04
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python進(jìn)程與線程操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》、《Python+MySQL數(shù)據(jù)庫(kù)程序設(shè)計(jì)入門教程》及《Python常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python中UserWarning:The NumPy module was
在 Python 項(xiàng)目中,我們經(jīng)常需要導(dǎo)入許多庫(kù)來(lái)完成各種任務(wù),NumPy 作為一個(gè)核心的科學(xué)計(jì)算庫(kù),被廣泛應(yīng)用于數(shù)據(jù)處理和分析,然而,有時(shí)我們會(huì)遇到 NumPy 重載的警告,本文將詳細(xì)講解這一警告的原因,并提供解決方案,需要的朋友可以參考下2024-07-07Python和C語(yǔ)言利用棧分別實(shí)現(xiàn)進(jìn)制轉(zhuǎn)換
這篇文章主要為大家詳細(xì)介紹了Python和C語(yǔ)言如何利用棧的數(shù)據(jù)結(jié)構(gòu)分別實(shí)現(xiàn)將十進(jìn)制數(shù)轉(zhuǎn)換成二進(jìn)制數(shù),文中的示例代碼講解詳細(xì),需要的可以參考一下2022-07-07Django入門優(yōu)缺點(diǎn)及環(huán)境搭建流程
這篇文章主要為大家介紹了Django入門優(yōu)缺點(diǎn)及環(huán)境搭建流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11