• <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++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

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

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

            上面的就是第一種做法,這種做法是很常見的,下面是另一種,曾經(jīng)使用過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
                
            # 創(chuàng)建鎖
                mutex = threading.Lock()
                
            # 創(chuàng)建線程對象
                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 秦歌
            好東東!
            久久久久波多野结衣高潮| 丁香狠狠色婷婷久久综合| 久久成人精品视频| 久久综合香蕉国产蜜臀AV| 国产成人无码精品久久久性色| 精品综合久久久久久88小说| 久久精品国产免费一区| 久久精品国产福利国产秒| 久久精品一区二区| 久久91精品综合国产首页| 精品久久综合1区2区3区激情| 国产精品免费久久久久电影网| 国产免费福利体检区久久| 国产呻吟久久久久久久92| 久久免费99精品国产自在现线| 亚洲欧洲精品成人久久奇米网| 免费精品久久天干天干| 精品久久久无码21p发布| 热re99久久精品国99热| 精品久久777| 久久本道综合久久伊人| 久久99国产精品久久99小说| 国产毛片欧美毛片久久久| 久久精品午夜一区二区福利 | 亚洲AV无码久久寂寞少妇| 国产综合久久久久| 久久久噜噜噜久久中文字幕色伊伊| 国产精品一区二区久久精品无码 | 久久久噜噜噜久久中文字幕色伊伊| 青草久久久国产线免观| 国产精品久久久久a影院| 久久综合综合久久综合| 国产精品成人99久久久久| 亚洲乱码日产精品a级毛片久久 | 精品无码久久久久久尤物| 青草影院天堂男人久久| 久久亚洲国产最新网站| 精品久久久久久| 久久久精品国产免大香伊| AAA级久久久精品无码区| 久久综合香蕉国产蜜臀AV|