自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

兩種Python線程編程方式簡介

開發(fā) 后端
本文詳細(xì)介紹Python線程編程的相關(guān)文章,因為目前大部分的腳本都不能提供如VC++那樣方便的調(diào)試環(huán)境,希望大家能夠?qū)W習(xí)介紹。

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

Python線程編程中如果要使用線程的話,python的lib中提供了兩種方式。一種是函數(shù)式,一種是用類來包裝的線程對象。舉兩個簡單的例子希望起到拋磚引玉的作用,關(guān)于多線程編程的其他知識例如互斥、信號量、臨界區(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個新的線程     
  10.       thread.start_new_thread(timer,(1,1))  
  11.       thread.start_new_thread(timer,(2,3))   
  12.  
  13.  
  14. if __name__=='__main__':   
  15.       test()  

這個是thread.start_new_thread(function,args[,kwargs])函數(shù)原型,其中function參數(shù)是你將要調(diào)用的線程函數(shù);args是講傳遞給你的線程函數(shù)的參數(shù),他必須是個tuple類型;而kwargs是可選的參數(shù),線程的結(jié)束一般依靠線程函數(shù)的自然結(jié)束;也可以在線程函數(shù)中調(diào)用thread.exit(),他拋出SystemExit exception,達(dá)到退出線程的目的。

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

  1. import threading    
  2. import time    
  3. class timer(threading.Thread):     #我的timer類繼承自threading.Thread類     
  4.     def __init__(self,no,interval):     
  5.         #在我重寫__init__方法的時候要記得調(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個線程對象     
  17.      threadtwo=timer(2,3)     
  18.      threadone.start()   #通過調(diào)用線程對象的.start()方法來激活線程     
  19.      threadtwo.start()     
  20.          
  21. if __name__=='__main__':     
  22.      test()   

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

【編輯推薦】

  1. 如何使Python嵌入C++應(yīng)用程序?
  2. 深入探討Ruby與Python語法比較
  3. Python學(xué)習(xí)資料介紹分享
  4. Python學(xué)習(xí)經(jīng)驗談:版本、IDE選擇及編碼解決方案
  5. 淺析Python的GIL和線程安全
責(zé)任編輯:chenqingxiang 來源: 計世網(wǎng)
相關(guān)推薦

2011-07-01 17:50:13

Python 多線程

2010-07-14 10:30:26

Perl多線程

2011-03-03 10:26:04

Pureftpd

2010-04-28 16:23:18

Oracle數(shù)據(jù)庫

2010-07-13 14:54:15

Perl面向?qū)ο缶幊?/a>

2010-09-07 11:09:59

2025-01-14 00:00:00

場景線程數(shù)據(jù)

2021-05-27 10:57:01

TCP定時器網(wǎng)絡(luò)協(xié)議

2009-06-25 13:43:00

Buffalo AJA

2010-10-21 16:24:18

sql server升

2010-08-06 09:38:11

Flex讀取XML

2023-03-29 13:06:36

2009-06-23 18:18:13

SpringHibernate

2010-04-20 15:32:20

主控負(fù)載均衡

2010-03-18 10:18:52

python模塊

2010-03-11 14:34:47

Python環(huán)境

2010-07-27 15:03:37

Flex ArrayC

2010-05-10 18:19:00

負(fù)載平衡技術(shù)

2024-09-20 11:32:28

.NET內(nèi)存管理

2011-03-23 11:22:14

oracle dbli
點贊
收藏

51CTO技術(shù)棧公眾號