• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            開源之路

            憶往昔, 項羽不過江. 江東好風光! 今振臂一呼,率甲三千, 試問天!
            posts - 86, comments - 55, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            python模塊之threading: 在python中使用多線程

            Posted on 2007-12-26 18:04 江邊之鳥 閱讀(10175) 評論(1)  編輯 收藏 引用 所屬分類: python
            python是支持多線程的,并且是native的線程。主要是通過thread和threading這兩個模塊來實現的。thread是比較底層的模 塊,threading是對thread做了一些包裝的,可以更加方便的被使用。這里需要提一下的是python對線程的支持還不夠完善,不能利用多 CPU,但是下個版本的python中已經考慮改進這點,讓我們拭目以待吧。
                threading模塊里面主要是對一些線程的操作對象化了,創建了叫Thread的class。一般來說,使用線程有兩種模式,一種是創建線程要執行的 函數,把這個函數傳遞進Thread對象里,讓它來執行;另一種是直接從Thread繼承,創建一個新的class,把線程執行的代碼放到這個新的 class里。我們來看看這兩種做法吧。

            #-*- encoding: gb2312 -*-
            import string, threading, time

            def thread_main(a):
                
            global count, mutex
                
            # 獲得線程名
                threadname = threading.currentThread().getName()
                
                
            for x in xrange(0, int(a)):
                    
            # 取得鎖
                    mutex.acquire()
                    count 
            = count + 1
                    
            # 釋放鎖
                    mutex.release()
                    
            print threadname, x, count
                    time.sleep(
            1)
                
            def main(num):
                
            global count, mutex
                threads 
            = []
                
                count 
            = 1
                
            # 創建一個鎖
                mutex = threading.Lock()
                
            # 先創建線程對象
                for x in xrange(0, num):
                    threads.append(threading.Thread(target
            =thread_main, args=(10,)))
                
            # 啟動所有線程
                for t in threads:
                    t.start()
                
            # 主線程中等待所有子線程退出
                for t in threads:
                    t.join()  
                
                
            if __name__ == '__main__':
                num 
            = 4
                
            # 創建4個線程
                main(4)

            上面的就是第一種做法,這種做法是很常見的,下面是另一種,曾經使用過Java的朋友應該很熟悉這種模式:

            #-*- encoding: gb2312 -*-
            import threading
            import time

            class Test(threading.Thread):
                
            def __init__(self, num):
                    threading.Thread.
            __init__(self)
                    self._run_num 
            = num
                
                
            def run(self):
                    
            global count, mutex
                    threadname 
            = threading.currentThread().getName()
                
                    
            for x in xrange(0, int(self._run_num)):
                        mutex.acquire()
                        count 
            = count + 1
                        mutex.release()
                        
            print threadname, x, count
                        time.sleep(
            1)

            if __name__ == '__main__':
                
            global count, mutex
                threads 
            = []
                num 
            = 4
                count 
            = 1
                
            # 創建鎖
                mutex = threading.Lock()
                
            # 創建線程對象
                for x in xrange(0, num):
                    threads.append(Test(
            10))
                
            # 啟動線程
                for t in threads:
                    t.start()
                
            # 等待子線程結束
                for t in threads:
                    t.join() 

            Feedback

            # re: python模塊之threading: 在python中使用多線程  回復  更多評論   

            2007-12-27 23:29 by 秦歌
            好東東!
            久久涩综合| 久久精品国产福利国产秒| 精品国产福利久久久| 亚洲伊人久久精品影院| 久久无码国产| 久久精品国产一区二区三区不卡| 国产精品天天影视久久综合网| 人妻无码久久一区二区三区免费 | 久久久久亚洲精品无码蜜桃| 综合久久给合久久狠狠狠97色| 亚洲色欲久久久久综合网| 久久综合久久美利坚合众国| 久久精品国产亚洲av麻豆蜜芽| 欧美成人免费观看久久| 久久精品综合网| 久久精品国产精品亚洲精品| 亚洲中文字幕无码一久久区| 久久精品天天中文字幕人妻| 久久久国产精品网站| 久久精品成人一区二区三区| 日韩欧美亚洲综合久久影院Ds| 亚洲欧洲精品成人久久奇米网| 性欧美大战久久久久久久久| 久久96国产精品久久久| yellow中文字幕久久网| 思思久久99热只有频精品66| 麻豆一区二区99久久久久| 91精品国产色综久久| 一本色道久久88综合日韩精品 | 天天综合久久一二三区| 亚洲乱码中文字幕久久孕妇黑人 | 国产精品一久久香蕉国产线看观看 | 麻豆av久久av盛宴av| 999久久久无码国产精品| 久久人人爽人爽人人爽av| 欧洲成人午夜精品无码区久久| 99热都是精品久久久久久| 国产精品久久久久久久人人看 | 国产成人精品久久| 99热都是精品久久久久久| 久久免费的精品国产V∧ |