• <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 江邊之鳥 閱讀(10183) 評論(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 秦歌
            好東東!
            久久久精品波多野结衣| 久久精品欧美日韩精品| 国产精品99久久精品爆乳| 国产精品欧美久久久久天天影视 | 亚洲国产精品无码久久一线| 一级做a爰片久久毛片毛片| 久久久久国产精品人妻| 国产精品99久久久久久人| 久久婷婷色综合一区二区| 亚洲精品乱码久久久久久蜜桃图片| 久久久久成人精品无码中文字幕| 久久se精品一区精品二区| 亚洲中文字幕伊人久久无码 | 久久亚洲精品中文字幕三区| 久久国产福利免费| 国产精品一久久香蕉国产线看观看| 99久久国产亚洲高清观看2024 | 久久亚洲欧洲国产综合| 天天爽天天狠久久久综合麻豆| 亚洲国产精久久久久久久| 伊人久久综合成人网| 久久国产精品视频| 国产精品一久久香蕉产线看| 久久精品国产欧美日韩99热| 91亚洲国产成人久久精品| 久久精品国产亚洲AV电影| 久久精品国产99国产精品亚洲| 国内精品久久久久久久亚洲| 久久中文娱乐网| 99久久精品国产麻豆| 久久精品aⅴ无码中文字字幕不卡| 日韩一区二区三区视频久久| 亚洲欧美成人综合久久久| 久久精品三级视频| 潮喷大喷水系列无码久久精品| 亚洲欧美成人久久综合中文网 | 精品国产乱码久久久久久1区2区 | 午夜欧美精品久久久久久久| 精品久久久久久无码不卡| 久久综合成人网| 深夜久久AAAAA级毛片免费看 |