日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
兩種Python線程編程方式簡介

由于Python線程編程的DEMO太多,此處無法上傳,所以大家有communitysever的可以從里面獲得然后反編譯為自己所用,沒有的就到網(wǎng)絡(luò)上搜下吧,有許多資源呢,僅供大家學(xué)習(xí)思考。

成都創(chuàng)新互聯(lián)公司是專業(yè)的布爾津網(wǎng)站建設(shè)公司,布爾津接單;提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行布爾津網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

Python線程編程中如果要使用線程的話,python的lib中提供了兩種方式。一種是函數(shù)式,一種是用類來包裝的線程對(duì)象。舉兩個(gè)簡單的例子希望起到拋磚引玉的作用,關(guān)于多線程編程的其他知識(shí)例如互斥、信號(hào)量、臨界區(qū)等請參考Python線程編程的文檔及相關(guān)資料。

1、調(diào)用thread模塊中的start_new_thread()函數(shù)來產(chǎn)生新的線程,請看代碼:

 
 
 
  1. # thread_example.py   
  2. import time   
  3. import thread   
  4. def timer(no,interval): #自己寫的線程函數(shù)   
  5.       while True:   
  6.             print 'Thread :(%d) Time:%s'%(no,time.ctime()) time.sleep(interval)   
  7.  
  8.  
  9. def test(): #使用thread.start_new_thread()來產(chǎn)生2個(gè)新的線程     
  10.       thread.start_new_thread(timer,(1,1))  
  11.       thread.start_new_thread(timer,(2,3))   
  12.  
  13.  
  14. if __name__=='__main__':   
  15.       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(),他拋出SystemExit exception,達(dá)到退出線程的目的。

2、通過調(diào)用threading模塊繼承threading.Thread類來包裝一個(gè)線程對(duì)象。請看代碼:

 
 
 
  1. import threading    
  2. import time    
  3. class timer(threading.Thread):     #我的timer類繼承自threading.Thread類     
  4.     def __init__(self,no,interval):     
  5.         #在我重寫__init__方法的時(shí)候要記得調(diào)用基類的__init__方法     
  6.         threading.Thread.__init__(self)          
  7.         self.no=no     
  8.         self.interval=interval     
  9.              
  10.     def run(self):  #重寫run()方法,把自己的線程函數(shù)的代碼放到這里     
  11.         while True:     
  12.             print 'Thread Object (%d), Time:%s'%(self.no,time.ctime())     
  13.             time.sleep(self.interval)     
  14.                  
  15. def test():     
  16.      threadone=timer(1,1)    #產(chǎn)生2個(gè)線程對(duì)象     
  17.      threadtwo=timer(2,3)     
  18.      threadone.start()   #通過調(diào)用線程對(duì)象的.start()方法來激活線程     
  19.      threadtwo.start()     
  20.          
  21. if __name__=='__main__':     
  22.      test()   

其實(shí)thread和threading的模塊中還包含了其他的很多關(guān)于多線程編程的東西,例如鎖、定時(shí)器、獲得激活線程列表等等,請大家仔細(xì)參考Python線程編程的文檔!

【編輯推薦】

  1. 如何使Python嵌入C++應(yīng)用程序?
  2. 深入探討Ruby與Python語法比較
  3. Python學(xué)習(xí)資料介紹分享
  4. Python學(xué)習(xí)經(jīng)驗(yàn)談:版本、IDE選擇及編碼解決方案
  5. 淺析Python的GIL和線程安全

當(dāng)前題目:兩種Python線程編程方式簡介
分享URL:http://www.5511xx.com/article/dpdeppc.html