<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      python類庫32[多線程]


       一 python 多線程

      因為CPython的實現使用了Global Interpereter Lock(GIL),使得python中同一時刻只有一個線程在執行,從而簡化了python解釋器的實現,且python對象模型天然地線程安全。如果你想你的應用程序在多核的機器上使用更好的資源,建議使用multiprocessing或concurrent.futures.processpoolexecutor。但是如果你的程序是IO密集型,則使用線程仍然是很好的選擇。 

       

      二 python 多線程使用的兩種方法

      實例:

       

      import threading
      import time

      def worker(num):
        print (threading.currentThread().getName() + ' start'
        time.sleep(10)
        print (threading.currentThread().getName() + ' running')
        print (threading.currentThread().getName() + " " + str(num))
        print (threading.currentThread().getName() + ' exit')
        
      def deamon():
        print (threading.currentThread().getName() + ' start')
        time.sleep(20)
        print (threading.currentThread().getName() + ' running')
        print (threading.currentThread().getName() + ' exit')
        
      print(threading.currentThread().getName())

      d = threading.Thread(name='deamon', target=deamon)
      d.setDaemon(True)
      d.start()

      w = threading.Thread(name='worker', target=worker, args=(10,))
      w.start()

      class myWorker(threading.Thread):
          def __init__(self, num):  
              threading.Thread.__init__(self)  
              self.num = num  
              self.thread_stop = False  
         
          def run(self): 
              print (self.getName()+' start')
              time.sleep(30)
              print (self.getName()+' running')
              print (self.getName()+" " + str(self.num))
              print (self.getName()+' exit')
       
      mw = myWorker(30)
      mw.setName("MyWorker")
      mw.start()

      print(threading.currentThread().getName())

      print("All threads:")
      print("------------")
      for th in threading.enumerate():
        print(th.getName())
      print("------------")

      d.join()
      w.join()
      mw.join()

      print(threading.currentThread().getName())

       

       

      運行結果如下:
       

       

      1)python線程使用的兩種方法:

      **直接調用threading.Thread來構造thread對象,Thread的參數如下:
      class threading.Thread(group=None, target=None, name=None, args=(), kwargs={})  
      group為None;
      target為線程將要執行的功能函數;
      name為線程的名字,也可以在對象構造后調用setName()來設定;
      args為tuple類型的參數,可以為多個,如果只有一個也的使用tuple的形式傳入,例如(1,);
      kwargs為dict類型的參數,也即位命名參數;

      **實現自己的threading.Thread的子類,需要重載__init__()和run()。

       

      2)threading.Thread對象的其他方法:

      start(),用來啟動線程;

      join(), 等待直到線程結束;

      setDeamon(), 設置線程為deamon線程,必須在start()調用前調用,默認為非demon。

      注意: python的主線程在沒有非deamon線程存在時就會退出。

       

      3)threading的靜態方法:

      threading.current_thread() , 用來獲得當前的線程;

      threading.enumerate() , 用來多的當前存活的所有線程;
      threading.Timer 定時器,其實是thread的一個字類型,使用如下: 

      def hello(): print("hello, world")   

      t = Timer(30.0, hello) 

      t.start()

       

      4)logging是線程安全的

      logging 模塊是線程安全的,所以可以使用logging來幫助調試多線程程序。
      import logging
      logging.basicConfig(level=logging.DEBUG,
      format="(%(threadName)-10s : %(message)s",
      )

      logging.debug("wait_for_event_timeout starting") 

       

       

      完! 

       

      posted @ 2012-01-04 17:33  iTech  閱讀(1930)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 免费午夜无码片在线观看影院| 日韩精品 在线 国产 丝袜| 国产黄色一区二区三区四区| 无码一区二区三区AV免费| 欧美一本大道香蕉综合视频| 日本高清在线播放一区二区三区 | 久久精品道一区二区三区| 成人网站网址导航| 99久久精品国产一区二区蜜芽| 久久国产乱子精品免费女| 麻豆国产高清精品国在线| 狠狠色丁香婷婷综合尤物| 夜夜夜高潮夜夜爽夜夜爰爰 | 国产精品免费无遮挡无码永久视频 | 人妻少妇精品中文字幕| 大尺度国产一区二区视频| 日韩精品永久免费播放平台| 亚洲AV日韩AV综合在线观看| 蜜臀一区二区三区精品免费| 暖暖影院日本高清...免费| 免费人妻无码不卡中文18禁| 少妇熟女高潮流白浆| 国产老熟女一区二区三区| 国产午夜精品福利91| 望城县| 国内精品伊人久久久久av| 国产嫩草精品网亚洲av| 国产普通话对白刺激| 久久精品一本到东京热| 国产日韩av二区三区| 国产综合精品一区二区三区| 九江市| 人妻一区二区三区三区| 国产在线线精品宅男网址| 国产线播放免费人成视频播放| 国产精品中文字幕自拍| 亚洲最大成人美女色av| 亚洲高潮喷水无码AV电影| 91精品国产蜜臀在线观看| 国内精品久久久久影院薰衣草| 久久精品国产99久久美女|