首先我們先來建立一個單線程中啟動並執行迴圈 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(),但在實際工作中我想沒有人這樣做吧)