<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] ### threading.Thread類 Thread是對thread模塊的封裝,開啟的子線程默認是非守護線程,不會隨主線程一切結束。 #### 常用方法 ``` start() #開始執行該線程 join(timeout=None) #子線程join到主線程 setName(name) --> thread.name getName() #取得線程名 is_alive()/isAlive() #線程是否存活的標志 setDaemon(False) --> thread.daemon = False isDaemon() activeCount()/active_count() # 當前活動對象個數 currentThread()/current_thread() # 返回當前Thread對象 enumerate() # 返回Thread對象列表 ``` #### threading.Thread的初始化函數 ``` def __init__(self, group=None, target=None, name=None, args=(), kwargs=None, verbose=None): ``` ![](http://i2.muimg.com/567571/06fdf903d612fc8a.png) ### 直接調用線程 `join`命名來源于posix標準。子線程join到主線程(啟動程序的線程,比如c語言執行main函數的線程)。你的問題可能在于沒有理解join,阻塞線程僅僅是一個表現,而非目的。其目的是等待當前線程執行完畢后,”計算單元”與主線程匯合。 ``` #!/usr/bin/env python # coding: utf-8 import threading from time import ctime,sleep def music(func): for i in range(5): print "I was listening to %s. %s" %(func,ctime()) sleep(1) def move(func): for i in range(1): print "I was at the %s! %s" %(func,ctime()) sleep(2) threads = [] t1 = threading.Thread(target=music, args=(u'愛情買賣',)) threads.append(t1) t2 = threading.Thread(target=move, args=(u'阿凡達',)) threads.append(t2) if __name__ == '__main__': for t in threads: t.setDaemon(False) t.start() # 等待線程結束,與主線程會和,這兩個線程都阻塞,主線程不會往下執行。 for t in threads: t.join() print "all over %s" %ctime() ``` ### 自定義線程類 ``` #!/usr/bin/python # coding: UTF-8 import threading import time class myThread(threading.Thread): #繼承父類threading.Thread def __init__(self, threadID, name, counter): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter def run(self): #把要執行的代碼寫到run函數里面 線程在創建后會直接運行run函數 print "Starting " + self.name printTime(self.name, self.counter) print "Exiting " + self.name def printTime(threadName, counter): for i in xrange(counter): time.sleep(1) print "%s: %s" % (threadName, time.ctime(time.time())) # 創建新線程 thread1 = myThread(1, "Thread-1", 3) thread2 = myThread(2, "Thread-2", 5) # 開啟線程 thread1.start() thread2.start() print "Exiting Main Thread" ``` #### 線程池 使用信號量充當線程池 下面的做法同樣會造成資源競爭,應該將信號量參數改為1 ``` #!/usr/bin/env python # coding: utf-8 import time from threading import Thread, Lock, BoundedSemaphore num = 0 def add(): time.sleep(1) with lock: global num num += 1 print num lock = BoundedSemaphore(1000) for i in range(10000): add_thread = Thread(target=add, args=()) add_thread.start() ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看