<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ### 導航 - [索引](../genindex.xhtml "總目錄") - [模塊](../py-modindex.xhtml "Python 模塊索引") | - [下一頁](_dummy_thread.xhtml "_dummy_thread --- _thread 的替代模塊") | - [上一頁](queue.xhtml "queue --- 一個同步的隊列類") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) ? - zh\_CN 3.7.3 [文檔](../index.xhtml) ? - [Python 標準庫](index.xhtml) ? - [并發執行](concurrency.xhtml) ? - $('.inline-search').show(0); | # [`_thread`](#module-_thread "_thread: Low-level threading API.") --- 底層多線程 API - - - - - - 該模塊提供了操作多個線程(也被稱為 *輕量級進程* 或 *任務*)的底層原語 —— 多個控制線程共享全局數據空間。為了處理同步問題,也提供了簡單的鎖機制(也稱為 *互斥鎖* 或 *二進制信號*)。[`threading`](threading.xhtml#module-threading "threading: Thread-based parallelism.") 模塊基于該模塊提供了更易用的高級多線程 API。 在 3.7 版更改: 這個模塊曾經是可選的,但現在總是可用的。 這個模塊定義了以下常量和函數: *exception* `_thread.``error`發生線程相關錯誤時拋出。 在 3.3 版更改: 現在是內建異常 [`RuntimeError`](exceptions.xhtml#RuntimeError "RuntimeError") 的別名。 `_thread.``LockType`鎖對象的類型。 `_thread.``start_new_thread`(*function*, *args*\[, *kwargs*\])啟動一個線程,并返回其標識符。線程會用 *args* 作為參數(必須是元組)執行 *function* 函數。可選的 *kwargs* 參數使用字典來指定有名參數。當函數返回時,線程會靜默退出,當函數由于未處理的異常而中止時,會打印一條堆棧追蹤信息,然后該線程會退出(但其他線程還是會繼續運行)。 `_thread.``interrupt_main`()在主線程中拋出 [`KeyboardInterrupt`](exceptions.xhtml#KeyboardInterrupt "KeyboardInterrupt") 異常。子線程可以使用這個函數來中斷主線程。 `_thread.``exit`()拋出 [`SystemExit`](exceptions.xhtml#SystemExit "SystemExit") 異常。如果沒有捕獲的話,這個異常會使線程退出。 `_thread.``allocate_lock`()返回一個新的鎖對象。鎖中的方法在后面描述。初始情況下鎖處于解鎖狀態。 `_thread.``get_ident`()返回當前線程的 “線程描述符”。它是一個非零的整型數。它的值沒有什么含義,主要是作為 magic cookie 使用,比如作為含有線程相關數據的字典的索引。線程描述符可能會在線程退出,新線程創建時復用。 `_thread.``stack_size`(\[*size*\])返回新建線程是用的堆棧大小。可選參數 *size* 指定之后新建的線程的堆棧大小,而且一定要是0(根據平臺或者默認配置)或者最小是32,768(32KiB)的一個正整數。如果\*size\*沒有指定,默認是0。如果不支持改變線程堆棧大小,會拋出 [`RuntimeError`](exceptions.xhtml#RuntimeError "RuntimeError") 錯誤。如果指定的堆棧大小不合法,會拋出 [`ValueError`](exceptions.xhtml#ValueError "ValueError") 錯誤并且不會修改堆棧大小。32KiB是當前最小的能保證解釋器足夠堆棧空間的堆棧大小。需要注意的是部分平臺對于堆棧大小會有特定的限制,例如要求大于32KiB的堆棧大小或者需要根據系統內存頁面的整數倍進行分配 - 應當查閱平臺文檔有關詳細信息(4KiB頁面比較普遍,在沒有更具體信息的情況下,建議的方法是使用4096的倍數作為堆棧大小) [可用性](intro.xhtml#availability): Windows,具有 POSIX 線程的系統。 `_thread.``TIMEOUT_MAX``Lock.acquire()` 方法中 *timeout* 參數允許的最大值。傳入超過這個值的 timeout 會拋出 [`OverflowError`](exceptions.xhtml#OverflowError "OverflowError") 異常。 3\.2 新版功能. 鎖對象有以下方法: `lock.``acquire`(*waitflag=1*, *timeout=-1*)沒有任何可選參數時,該方法無條件申請獲得鎖,有必要的話會等待其他線程釋放鎖(同時只有一個線程能獲得鎖 —— 這正是鎖存在的原因)。 如果傳入了整型參數 *waitflag*,具體的行為取決于傳入的值:如果是 0 的話,只會在能夠立刻獲取到鎖時才獲取,不會等待,如果是非零的話,會像之前提到的一樣,無條件獲取鎖。 如果傳入正浮點數參數 *timeout*,相當于指定了返回之前等待得最大秒數。如果傳入負的 *timeout*,相當于無限期等待。如果 *waitflag* 是 0 的話,不能指定 *timeout*。 如果成功獲取到所會返回 `True`,否則返回 `False`。 在 3.2 版更改: *timeout* 形參是新增的。 在 3.2 版更改: 現在獲取鎖的操作可以被 POSIX 信號中斷。 `lock.``release`()釋放鎖。鎖必須已經被獲取過,但不一定是同一個線程獲取的。 `lock.``locked`()返回鎖的狀態:如果已被某個線程獲取,返回 `True`,否則返回 `False`。 除了這些方法之外,鎖對象也可以通過 [`with`](../reference/compound_stmts.xhtml#with) 語句使用,例如: ``` import _thread a_lock = _thread.allocate_lock() with a_lock: print("a_lock is locked while this executes") ``` **注意事項:** > - 線程與中斷奇怪地交互:[`KeyboardInterrupt`](exceptions.xhtml#KeyboardInterrupt "KeyboardInterrupt") 異常可能會被任意一個線程捕獲。(如果 [`signal`](signal.xhtml#module-signal "signal: Set handlers for asynchronous events.") 模塊可用的話,中斷總是會進入主線程。) - 調用 [`sys.exit()`](sys.xhtml#sys.exit "sys.exit") 或是拋出 [`SystemExit`](exceptions.xhtml#SystemExit "SystemExit") 異常等效于調用 [`_thread.exit()`](#_thread.exit "_thread.exit")。 - 不可能中斷鎖的 `acquire()` 方法 —— [`KeyboardInterrupt`](exceptions.xhtml#KeyboardInterrupt "KeyboardInterrupt") 一場會在鎖獲取到之后發生。 - 當主線程退出時,由系統決定其他線程是否存活。在大多數系統中,這些線程會直接被殺掉,不會執行 [`try`](../reference/compound_stmts.xhtml#try) ... [`finally`](../reference/compound_stmts.xhtml#finally) 語句,也不會執行對象析構函數。 - 當主線程退出時,不會進行正常的清理工作(除非使用了 [`try`](../reference/compound_stmts.xhtml#try) ... [`finally`](../reference/compound_stmts.xhtml#finally) 語句),標準 I/O 文件也不會刷新。 ### 導航 - [索引](../genindex.xhtml "總目錄") - [模塊](../py-modindex.xhtml "Python 模塊索引") | - [下一頁](_dummy_thread.xhtml "_dummy_thread --- _thread 的替代模塊") | - [上一頁](queue.xhtml "queue --- 一個同步的隊列類") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) ? - zh\_CN 3.7.3 [文檔](../index.xhtml) ? - [Python 標準庫](index.xhtml) ? - [并發執行](concurrency.xhtml) ? - $('.inline-search').show(0); | ? [版權所有](../copyright.xhtml) 2001-2019, Python Software Foundation. Python 軟件基金會是一個非盈利組織。 [請捐助。](https://www.python.org/psf/donations/) 最后更新于 5月 21, 2019. [發現了問題](../bugs.xhtml)? 使用[Sphinx](http://sphinx.pocoo.org/)1.8.4 創建。
                  <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>

                              哎呀哎呀视频在线观看