python 多線程編程之threading

來源:互聯網
上載者:User

    首先我們先來建立一個單線程中啟動並執行迴圈 onethr.py

    '''<br />Created on 2010-12-16<br />@author: Administrator<br />'''<br />from time import sleep,ctime<br />def loop0():<br /> print 'start loop 0 at:',ctime()<br /> sleep(4)<br /> print 'loop 0 done at:',ctime()<br />def loop1():<br /> print 'start loop 1 at:',ctime()<br /> sleep(2)<br /> print 'loop done at:',ctime()</p><p>def main():<br /> print 'starting at:',ctime()<br /> loop0()<br /> loop1()<br /> print 'all DONE at:',ctime()<br />if __name__=='__main__':<br /> main() 

 

 下面是用多線程完成上面單線程的功能

  '''<br />Created on 2010-12-16<br />@author: Administrator<br />'''<br />import threading<br />from time import sleep,ctime<br />loops=[4,2]<br />class MyThread(threading.Thread):<br /> def __init__(self,func,args,name=''):<br /> threading.Thread.__init__(self)<br /> self.name=name<br /> self.func=func<br /> self.args=args<br /> def run(self):<br /> apply(self.func,self.args)</p><p>def loop(nloop,nsec):<br /> print 'start loop',nloop,'at:',ctime()<br /> sleep(nsec)<br /> print 'loop',nloop,'done at:',ctime()</p><p>def main():<br /> print 'starting at:',ctime()<br /> threads=[]<br /> nloops=range(len(loops))</p><p> for i in nloops:<br /> t=MyThread(loop,(i,loops[i]),loop.__name__)<br /> threads.append(t)</p><p> for i in nloops:<br /> threads[i].start()<br /> for i in nloops:<br /> threads[i].join()<br /> print 'all done at:',ctime()<br />if __name__=='__main__':<br /> main()

 

 下面是mtfacfib.py 指令碼比較了遞迴求斐波那契,階乘和累加和函數的運行。指令碼先在 

 單線程中運行這三個函數,然後在多線程中做同樣的事,以說明多線程的好處 

 

 首先我們要完成一個通用的線程類myThread.py

 '''<br />Created on 2010-12-16<br />@author: Administrator<br />'''<br />import threading<br />from time import ctime<br />class MyThread(threading.Thread):<br /> def __init__(self,func,args,name=''):<br /> threading.Thread.__init__(self)<br /> self.name=name<br /> self.func=func<br /> self.args=args<br /> def getResult(self):<br /> return self.res<br /> def run(self):<br /> print 'starting',self.name,'at:',ctime()<br /> self.res=apply(self.func,self.args)<br /> print self.name,'finished at:',ctime() 

 

mtfacfib.py

'''<br />Created on 2010-12-16<br />@author: Administrator<br />'''<br />from time import ctime,sleep<br />from myThread import MyThread<br />def fib(x):<br /> sleep(0.005)<br /> if x<2:return 1<br /> return (fib(x-2)+fib(x-1))<br />def fac(x):<br /> sleep(0.1)<br /> if x<2:return 1<br /> return (x*fac(x-1))<br />def sum(x):<br /> sleep(0.1)<br /> if x<2:return 1<br /> return (x+sum(x-1))<br />funcs=[fib,fac,sum]<br />n=12<br />def main():<br /> nfuncs=range(len(funcs))<br /> print '*** SINGLE THREAD'<br /> for i in nfuncs:<br /> print 'starting',funcs[i].__name__,'at:',ctime()<br /> print funcs[i](n)<br /> print funcs[i].__name__,'finished at:',ctime()<br /> print '***MULTIPLE THREADS'<br /> threads=[]<br /> for i in nfuncs:<br /> t=MyThread(funcs[i],(n,),funcs[i].__name__)<br /> threads.append(t)<br /> for i in nfuncs:<br /> threads[i].start()<br /> for i in nfuncs:<br /> threads[i].join()<br /> print threads[i].getResult()<br /> print 'all DONE'</p><p>if __name__=='__main__':<br /> main()</p><p> 

 

運行結果:

 

我們可以看出多線程對比單線程給我帶來具大的效能的提升(本例為了對比效果所以設定了sleep(),但在實際工作中我想沒有人這樣做吧)

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.