<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] # 簡介 由于線程是操作系統直接支持的執行單元,因此,高級語言通常都內置多線程的支持,Python也不例外,并且,Python的線程是真正的Posix Thread,而不是模擬出來的線程. Python的標準庫提供了兩個模塊:`_thread`和threading,`_thread`是低級模塊,threading是高級模塊,對_thread進行了封裝。絕大多數情況下,我們只需要使用threading這個高級模塊 啟動一個線程就是把一個函數傳入并創建Thread實例,然后調用start()開始執行 # 線程名字 由于任何進程默認就會啟動一個線程,我們把該線程稱為主線程,主線程又可以啟動新的線程,Python的threading模塊有個`current_thread()`函數,它永遠返回當前線程的實例。主線程實例的名字叫MainThread. 子線程的名字在創建時指定,我們用LoopThread命名子線程。名字僅僅在打印時用來顯示,完全沒有其他意義,如果不起名字Python就自動給線程命名為Thread-1,Thread-2…… # 常用方法 在一個新線程中運行loop方法,線程名字叫LoopThread,args表示給這個方法傳遞參數 ~~~ threading.Thread(target=loop, name='LoopThread', args=(g_nums, )) ~~~ 當前線程的名字 ~~~ threading.current_thread().name ~~~ 查看線程的數量 ~~~ len(threading.enumerate()) ~~~ # 多線程,函數函數 ~~~ import threading # 新線程執行的代碼: def loop(): print('thread %s is running...' % threading.current_thread().name) print('thread %s is running...' % threading.current_thread().name) t = threading.Thread(target=loop, name='LoopThread') # 啟動線程 t.start() ~~~ 當調用start()時,才會真正的創建線程,并開始執行 # 查看線程的數量 ~~~ import threading # 新線程執行的代碼: def loop(): print('thread %s is running...' % threading.current_thread().name) print('thread %s is running...' % threading.current_thread().name) t1 = threading.Thread(target=loop, name='t1') t2 = threading.Thread(target=loop, name='t2') t1.start() t2.start() length = len(threading.enumerate()) print('當前運行的線程數為 : %d' %length) ~~~ # 多線程,對象方式 ~~~ import threading, time class MyThread(threading.Thread): def run(self): for i in range(3): time.sleep(1) msg = "I'm " + self.name + ' @ ' + str(i) # name屬性中保存的是當前線程的名字 print(msg) if __name__ == '__main__': t = MyThread() # start會自動執行run方法 t.start() ~~~ # lock **常用方法** ~~~ # 創建鎖 mutex = threading.Lock() # 鎖定 mutex.acquire() # 釋放 mutex.release() ~~~ **注意** * 如果這個鎖之前是沒有上鎖的,那么acquire不會阻塞 * 如果在調用acquire對這個鎖上鎖之前,它已經被其他線程上了鎖,那么此時acquire鎖被解鎖為止 # 多核CPU 如果你不幸擁有一個多核CPU,你肯定在想,多核應該可以同時執行多個線程。 如果寫一個死循環的話,會出現什么情況呢? 打開Mac OS X的Activity Monitor,或者Windows的Task Manager,都可以監控某個進程的CPU使用率。 我們可以監控到一個死循環線程會100%占用一個CPU。 如果有兩個死循環線程,在多核CPU中,可以監控到會占用200%的CPU,也就是占用兩個CPU核心。 要想把N核CPU的核心全部跑滿,就必須啟動N個死循環線程。 試試用Python寫個死循環: ~~~ import threading, multiprocessing def loop(): x = 0 while True: x = x ^ 1 for i in range(multiprocessing.cpu_count()): t = threading.Thread(target=loop) t.start() ~~~ 啟動與CPU核心數量相同的N個線程,在4核CPU上可以監控到CPU占用率僅有102%,也就是僅使用了一核。 但是用C、C++或Java來改寫相同的死循環,直接可以把全部核心跑滿,4核就跑到400%,8核就跑到800%,為什么Python不行呢? 因為Python的線程雖然是真正的線程,但解釋器執行代碼時,有一個GIL鎖:Global Interpreter Lock,任何Python線程執行前,必須先獲得GIL鎖,然后,每執行100條字節碼,解釋器就自動釋放GIL鎖,讓別的線程有機會執行。這個GIL全局鎖實際上把所有線程的執行代碼都給上了鎖,所以,多線程在Python中只能交替執行,即使100個線程跑在100核CPU上,也只能用到1個核。 GIL是Python解釋器設計的歷史遺留問題,通常我們用的解釋器是官方實現的CPython,要真正利用多核,除非重寫一個不帶GIL的解釋器。 所以,在Python中,可以使用多線程,但不要指望能有效利用多核。如果一定要通過多線程利用多核,那只能通過C擴展來實現,不過這樣就失去了Python簡單易用的特點。 不過,也不用過于擔心,Python雖然不能利用多線程實現多核任務,但可以通過多進程實現多核任務。多個Python進程有各自獨立的GIL鎖,互不影響
                  <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>

                              哎呀哎呀视频在线观看