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。一般來說,使用線程有兩種模式:
- A 創(chuàng)建線程要執(zhí)行的函數(shù),把這個(gè)函數(shù)傳遞進(jìn)Thread對(duì)象里,讓它來執(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ù)庫程序設(shè)計(jì)入門教程》及《Python常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python替換NumPy數(shù)組中大于某個(gè)值的所有元素實(shí)例
這篇文章主要介紹了Python替換NumPy數(shù)組中大于某個(gè)值的所有元素實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06
python數(shù)據(jù)分析apply(),map(),applymap()用法
這篇文章主要介紹了python數(shù)據(jù)分析apply(),map(),applymap()用法,可以方便地實(shí)現(xiàn)對(duì)批量數(shù)據(jù)的自定義操作。用法歸納如下,需要的朋友可以參考一下2022-03-03
flask + pymysql操作Mysql數(shù)據(jù)庫的實(shí)例
下面小編就為大家?guī)硪黄猣lask + pymysql操作Mysql數(shù)據(jù)庫的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
Python成功解決TypeError: ‘method’ object is
在Python編程中,有時(shí)候我們可能會(huì)遇到一個(gè)讓人摸不著頭腦的錯(cuò)誤信息:TypeError: 'method' object is not subscriptable,本文給大家介紹了Python如何成功解決TypeError: ‘method’ object is not subscriptable,需要的朋友可以參考下2024-06-06

