<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] ## thread模塊 較底層次線程模塊 使用thread模塊開啟的線程,會隨主線程`一起結束`;為了讓子線程能夠執行完任務,所以可以讓主線程睡眠或者給主線程加鎖,等待子線程結束后再釋放鎖。 ```python #!/usr/bin/env python #coding: utf-8 #python2 """為了不讓主進程執行完就退出,給主線程加鎖,當子線程執行結束后,釋放主線程的鎖 存在的問題: 當兩個線程執行次數不一致時,先結束的線程即會釋放主線程的鎖,導致未執行完的線程被迫退出 """ import thread import time def processor(thread_name, times): for i in range(times): print i, thread_name time.sleep(0.001) # for循環結束,釋放鎖 lock.release() lock = thread.allocate_lock() lock.acquire() thread.start_new_thread(processor, ('image', 5)) thread.start_new_thread(processor, ('sound', 3)) while lock.locked(): pass ``` ### 當線程間的執行需要一定次序時,則需要給子線程也加鎖。 當主線程,hello線程和world線程,執行到需要獲取鎖的時候,如果獲取不到鎖,則阻塞在那里;world線程在獲取鎖 world_thread_lock的時候阻塞了,當hello線程釋放了 鎖world_thread_lock,輸出了‘world’;此時hello線程也在獲取鎖過程中阻塞了,所以當world線程釋放了hello_thread_lock,輸出了hello,這樣交替釋放彼此的鎖,實現了順序運行 ![](http://i2.muimg.com/567571/5ef5968d58a2be35.png) ```python #!/usr/bin/env python #coding: utf-8 #python2 import time import thread def hello(): for i in xrange(5): hello_thread_lock.acquire() print 'hello', time.time(), world_thread_lock.release() def world(): for i in xrange(5): world_thread_lock.acquire() print 'world', time.time() hello_thread_lock.release() time.sleep(1) # 釋放主進程的鎖 main_thread_lock.release() main_thread_lock = thread.allocate_lock() main_thread_lock.acquire() hello_thread_lock = thread.allocate_lock() world_thread_lock = thread.allocate_lock() world_thread_lock.acquire() thread.start_new_thread(hello, ()) thread.start_new_thread(world, ()) while main_thread_lock.locked(): pass ``` ```python #!/usr/bin/env python # coding: utf8 import thread from time import sleep, ctime loops = [4, 2] def loop(nloop, nsec, lock): print 'start loop', nloop, 'at:', ctime() sleep(nsec) print 'loop', nloop, 'done at:', ctime() lock.release() def main(): print 'starting threads...' locks = [] loops_len = range(len(loops)) # 生成鎖列表 for i in loops_len: lock = thread.allocate_lock() lock.acquire() locks.append(lock) # 開啟進程 for i in loops_len: thread.start_new_thread(loop, (i, loops[i], locks[i])) # 等待所有的lock都釋放 for i in loops_len: while locks[i].locked(): pass print 'all DONE at:', ctime() if __name__ == '__main__': main() ```
                  <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>

                              哎呀哎呀视频在线观看