“Python/docs/3.9/library/ thread”的版本间差异

来自菜鸟教程
Python/docs/3.9/library/ thread
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:_thread — 低级线程 API — Python 文档}}
 
<div id="module-_thread" class="section">
 
<div id="module-_thread" class="section">
  
 
<span id="thread-low-level-threading-api"></span>
 
<span id="thread-low-level-threading-api"></span>
= [[#module-_thread|<code>_thread</code>]] --- Low-level threading API =
+
= _thread — 低级线程 API =
  
This module provides low-level primitives for working with multiple threads
+
 
(also called ''light-weight processes'' or ''tasks'') --- multiple threads of
+
-----
control sharing their global data space. For synchronization, simple locks
+
 
(also called ''mutexes'' or ''binary semaphores'') are provided.
+
该模块提供用于处理多线程(也称为 ''轻量级进程'' ''任务'' )的低级原语——多个控制线程共享其全局数据空间。 对于同步,提供了简单的锁(也称为 ''互斥锁'' ''二进制信号量'' )。 [[../threading#module-threading|threading]] 模块提供了一个更易于使用的高级线程 API,该 API 构建在该模块之上。
The [[../threading#module-threading|<code>threading</code>]] module provides an easier to use and higher-level
 
threading API built on top of this module.
 
  
 
<div id="index-1" class="versionchanged">
 
<div id="index-1" class="versionchanged">
  
<span class="versionmodified changed">3.7 版更改: </span>This module used to be optional, it is now always available.
+
<span class="versionmodified changed"> 3.7 版本变更: </span> 该模块以前是可选的,现在一直可用。
  
  
 
</div>
 
</div>
This module defines the following constants and functions:
+
该模块定义了以下常量和函数:
  
 
<dl>
 
<dl>
<dt><span id="_thread.error"></span>''exception'' <code>_thread.</code><code>error</code></dt>
+
<dt><span id="_thread.error"></span>''<span class="pre">exception</span>'' <span class="sig-prename descclassname"><span class="pre">_thread.</span></span><span class="sig-name descname"><span class="pre">error</span></span></dt>
<dd><p>Raised on thread-specific errors.</p>
+
<dd><p>引发线程特定的错误。</p>
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">3.3 版更改: </span>This is now a synonym of the built-in [[../exceptions#RuntimeError|<code>RuntimeError</code>]].</p>
+
<p><span class="versionmodified changed"> 3.3 版更改: </span> 现在是内置 [[../exceptions#RuntimeError|RuntimeError]] 的同义词。</p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
; <span id="_thread.LockType"></span><code>_thread.</code><code>LockType</code>
+
; <span id="_thread.LockType"></span><span class="sig-prename descclassname"><span class="pre">_thread.</span></span><span class="sig-name descname"><span class="pre">LockType</span></span>
: This is the type of lock objects.
+
: 这是锁对象的类型。
  
 
<dl>
 
<dl>
<dt><span id="_thread.start_new_thread"></span><code>_thread.</code><code>start_new_thread</code><span class="sig-paren">(</span>''function'', ''args''<span class="optional">[</span>, ''kwargs''<span class="optional">]</span><span class="sig-paren">)</span></dt>
+
<dt><span id="_thread.start_new_thread"></span><span class="sig-prename descclassname"><span class="pre">_thread.</span></span><span class="sig-name descname"><span class="pre">start_new_thread</span></span><span class="sig-paren">(</span>''<span class="pre">function</span>'', ''<span class="pre">args</span>''<span class="optional">[</span>, ''<span class="pre">kwargs</span>''<span class="optional">]</span><span class="sig-paren">)</span></dt>
<dd><p>Start a new thread and return its identifier. The thread executes the
+
<dd><p>启动一个新线程并返回其标识符。 线程使用参数列表 ''args''(必须是元组)执行函数 ''function''。 可选的 ''kwargs'' 参数指定关键字参数的字典。</p>
function ''function'' with the argument list ''args'' (which must be a tuple).
+
<p>当函数返回时,线程默默地退出。</p>
The optional ''kwargs'' argument specifies a dictionary of keyword arguments.</p>
+
<p>当函数因未处理的异常而终止时,会调用 [[../sys#sys|sys.unraisablehook()]] 来处理异常。 钩子参数的 ''object'' 属性是 ''function''。 默认情况下,打印堆栈跟踪,然后线程退出(但其他线程继续运行)。</p>
<p>When the function returns, the thread silently exits.</p>
+
<p>当函数引发 [[../exceptions#SystemExit|SystemExit]] 异常时,它会被静默忽略。</p>
<p>When the function terminates with an unhandled exception,
 
[[../sys#sys|<code>sys.unraisablehook()</code>]] is called to handle the exception. The ''object''
 
attribute of the hook argument is ''function''. By default, a stack trace is
 
printed and then the thread exits (but other threads continue to run).</p>
 
<p>When the function raises a [[../exceptions#SystemExit|<code>SystemExit</code>]] exception, it is silently
 
ignored.</p>
 
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">3.8 版更改: </span>[[../sys#sys|<code>sys.unraisablehook()</code>]] is now used to handle unhandled exceptions.</p>
+
<p><span class="versionmodified changed"> 3.8 版更改: </span>[[../sys#sys|sys.unraisablehook()]] 现在用于处理未处理的异常。</p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt><span id="_thread.interrupt_main"></span><code>_thread.</code><code>interrupt_main</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
+
<dt><span id="_thread.interrupt_main"></span><span class="sig-prename descclassname"><span class="pre">_thread.</span></span><span class="sig-name descname"><span class="pre">interrupt_main</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
<dd><p>Simulate the effect of a [[../signal#signal|<code>signal.SIGINT</code>]] signal arriving in the main
+
<dd><p>模拟一个 [[../signal#signal|signal.SIGINT]] 信号到达主线程的效果。 一个线程可以使用这个函数来中断主线程。</p>
thread. A thread can use this function to interrupt the main thread.</p>
+
<p>如果 [[../signal#signal|signal.SIGINT]] 没有被 Python 处理(它被设置为 [[../signal#signal|signal.SIG_DFL]] [[../signal#signal|signal.SIG_IGN]]),这个函数什么都不做。</p></dd></dl>
<p>If [[../signal#signal|<code>signal.SIGINT</code>]] isn't handled by Python (it was set to
 
[[../signal#signal|<code>signal.SIG_DFL</code>]] or [[../signal#signal|<code>signal.SIG_IGN</code>]]), this function does
 
nothing.</p></dd></dl>
 
  
; <span id="_thread.exit"></span><code>_thread.</code><code>exit</code><span class="sig-paren">(</span><span class="sig-paren">)</span>
+
; <span id="_thread.exit"></span><span class="sig-prename descclassname"><span class="pre">_thread.</span></span><span class="sig-name descname"><span class="pre">exit</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span>
: Raise the [[../exceptions#SystemExit|<code>SystemExit</code>]] exception. When not caught, this will cause the thread to exit silently.
+
: 引发 [[../exceptions#SystemExit|SystemExit]] 异常。 如果没有被捕获,这将导致线程静默退出。
  
; <span id="_thread.allocate_lock"></span><code>_thread.</code><code>allocate_lock</code><span class="sig-paren">(</span><span class="sig-paren">)</span>
+
; <span id="_thread.allocate_lock"></span><span class="sig-prename descclassname"><span class="pre">_thread.</span></span><span class="sig-name descname"><span class="pre">allocate_lock</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span>
: Return a new lock object. Methods of locks are described below. The lock is initially unlocked.
+
: 返回一个新的锁对象。 下面描述了锁的方法。 锁最初是解锁的。
  
; <span id="_thread.get_ident"></span><code>_thread.</code><code>get_ident</code><span class="sig-paren">(</span><span class="sig-paren">)</span>
+
; <span id="_thread.get_ident"></span><span class="sig-prename descclassname"><span class="pre">_thread.</span></span><span class="sig-name descname"><span class="pre">get_ident</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span>
: Return the 'thread identifier' of the current thread. This is a nonzero integer. Its value has no direct meaning; it is intended as a magic cookie to be used e.g. to index a dictionary of thread-specific data. Thread identifiers may be recycled when a thread exits and another thread is created.
+
: 返回当前线程的“线程标识符”。 这是一个非零整数。 它的价值没有直接的意义; 它旨在用作魔术饼干,例如 索引线程特定数据的字典。 当一个线程退出并创建另一个线程时,线程标识符可能会被回收。
  
 
<dl>
 
<dl>
<dt><span id="_thread.get_native_id"></span><code>_thread.</code><code>get_native_id</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
+
<dt><span id="_thread.get_native_id"></span><span class="sig-prename descclassname"><span class="pre">_thread.</span></span><span class="sig-name descname"><span class="pre">get_native_id</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
<dd><p>Return the native integral Thread ID of the current thread assigned by the kernel.
+
<dd><p>返回内核分配的当前线程的本机整数线程 ID。 这是一个非负整数。 它的值可用于在系统范围内唯一标识此特定线程(直到线程终止,之后该值可由操作系统回收)。</p>
This is a non-negative integer.
 
Its value may be used to uniquely identify this particular thread system-wide
 
(until the thread terminates, after which the value may be recycled by the OS).</p>
 
<p>[[../intro#availability|<span class="std std-ref">Availability</span>]]: Windows, FreeBSD, Linux, macOS, OpenBSD, NetBSD, AIX.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.8 新版功能.</span></p>
+
<p><span class="versionmodified added">3.8 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
<dl>
+
; <span id="_thread.stack_size"></span><span class="sig-prename descclassname"><span class="pre">_thread.</span></span><span class="sig-name descname"><span class="pre">stack_size</span></span><span class="sig-paren">(</span><span class="optional">[</span>''<span class="pre">size</span>''<span class="optional">]</span><span class="sig-paren">)</span>
<dt><span id="_thread.stack_size"></span><code>_thread.</code><code>stack_size</code><span class="sig-paren">(</span><span class="optional">[</span>''size''<span class="optional">]</span><span class="sig-paren">)</span></dt>
+
: 返回创建新线程时使用的线程堆栈大小。 可选的 ''size'' 参数指定用于后续创建的线程的堆栈大小,并且必须为 0(使用平台或配置的默认值)或至少为 32,768 (32 KiB) 的正整数值。 如果未指定 ''size'',则使用 0。 如果不支持更改线程堆栈大小,则会引发 [[../exceptions#RuntimeError|RuntimeError]]。 如果指定的堆栈大小无效,则会引发 [[../exceptions#ValueError|ValueError]] 并且堆栈大小未修改。 32 KiB 目前是支持的最小堆栈大小值,以保证解释器本身有足够的堆栈空间。 请注意,某些平台可能对堆栈大小的值有特定限制,例如要求最小堆栈大小 &gt; 32 KiB 或要求以系统内存页面大小的倍数进行分配 - 应参考平台文档以获取更多信息(4 KiB 页很常见;如果没有更具体的信息,建议使用 4096 的倍数作为堆栈大小)。
<dd><p>Return the thread stack size used when creating new threads. The optional
 
''size'' argument specifies the stack size to be used for subsequently created
 
threads, and must be 0 (use platform or configured default) or a positive
 
integer value of at least 32,768 (32 KiB). If ''size'' is not specified,
 
0 is used. If changing the thread stack size is
 
unsupported, a [[../exceptions#RuntimeError|<code>RuntimeError</code>]] is raised. If the specified stack size is
 
invalid, a [[../exceptions#ValueError|<code>ValueError</code>]] is raised and the stack size is unmodified. 32 KiB
 
is currently the minimum supported stack size value to guarantee sufficient
 
stack space for the interpreter itself. Note that some platforms may have
 
particular restrictions on values for the stack size, such as requiring a
 
minimum stack size &gt; 32 KiB or requiring allocation in multiples of the system
 
memory page size - platform documentation should be referred to for more
 
information (4 KiB pages are common; using multiples of 4096 for the stack size is
 
the suggested approach in the absence of more specific information).</p>
 
<p>[[../intro#availability|<span class="std std-ref">Availability</span>]]: Windows, systems with POSIX threads.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt><span id="_thread.TIMEOUT_MAX"></span><code>_thread.</code><code>TIMEOUT_MAX</code></dt>
+
<dt><span id="_thread.TIMEOUT_MAX"></span><span class="sig-prename descclassname"><span class="pre">_thread.</span></span><span class="sig-name descname"><span class="pre">TIMEOUT_MAX</span></span></dt>
<dd><p>The maximum value allowed for the ''timeout'' parameter of
+
<dd><p><code>Lock.acquire()</code> 的 ''timeout'' 参数允许的最大值。 指定大于此值的超时将引发 [[../exceptions#OverflowError|OverflowError]]</p>
<code>Lock.acquire()</code>. Specifying a timeout greater than this value will
 
raise an [[../exceptions#OverflowError|<code>OverflowError</code>]].</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.2 新版功能.</span></p>
+
<p><span class="versionmodified added">3.2 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
Lock objects have the following methods:
+
锁对象有以下方法:
  
 
<dl>
 
<dl>
<dt><span id="_thread.lock.acquire"></span><code>lock.</code><code>acquire</code><span class="sig-paren">(</span>''<span class="n">waitflag</span><span class="o">=</span><span class="default_value">1</span>'', ''<span class="n">timeout</span><span class="o">=</span><span class="default_value">- 1</span>''<span class="sig-paren">)</span></dt>
+
<dt><span id="_thread.lock.acquire"></span><span class="sig-prename descclassname"><span class="pre">lock.</span></span><span class="sig-name descname"><span class="pre">acquire</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">waitflag</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span>'', ''<span class="n"><span class="pre">timeout</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-</span> <span class="pre">1</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Without any optional argument, this method acquires the lock unconditionally, if
+
<dd><p>在没有任何可选参数的情况下,此方法无条件地获取锁,如有必要,等待它被另一个线程释放(一次只有一个线程可以获取锁——这就是它们存在的原因)。</p>
necessary waiting until it is released by another thread (only one thread at a
+
<p>如果存在整数 ''waitflag'' 参数,则操作取决于其值:如果为零,则仅在无需等待即可立即获取时才获取锁,如果不为零,则获取锁无条件如上。</p>
time can acquire a lock --- that's their reason for existence).</p>
+
<p>如果浮点 ''timeout'' 参数存在且为正,则它指定返回前的最大等待时间(以秒为单位)。 负 ''timeout'' 参数指定无限等待。 如果 ''waitflag'' 为零,则不能指定 ''timeout''</p>
<p>If the integer ''waitflag'' argument is present, the action depends on its
+
<p>获取锁成功返回值为<code>True</code>,否则返回值为<code>False</code></p>
value: if it is zero, the lock is only acquired if it can be acquired
 
immediately without waiting, while if it is nonzero, the lock is acquired
 
unconditionally as above.</p>
 
<p>If the floating-point ''timeout'' argument is present and positive, it
 
specifies the maximum wait time in seconds before returning. A negative
 
''timeout'' argument specifies an unbounded wait. You cannot specify
 
a ''timeout'' if ''waitflag'' is zero.</p>
 
<p>The return value is <code>True</code> if the lock is acquired successfully,
 
<code>False</code> if not.</p>
 
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">3.2 版更改: </span>The ''timeout'' parameter is new.</p>
+
<p><span class="versionmodified changed"> 3.2 版更改: </span> ''timeout'' 参数是新的。</p>
  
 
</div>
 
</div>
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">3.2 版更改: </span>Lock acquires can now be interrupted by signals on POSIX.</p>
+
<p><span class="versionmodified changed"> 3.2 版更改: </span> 锁定获取现在可以被 POSIX 上的信号中断。</p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
; <span id="_thread.lock.release"></span><code>lock.</code><code>release</code><span class="sig-paren">(</span><span class="sig-paren">)</span>
+
; <span id="_thread.lock.release"></span><span class="sig-prename descclassname"><span class="pre">lock.</span></span><span class="sig-name descname"><span class="pre">release</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span>
: Releases the lock. The lock must have been acquired earlier, but not necessarily by the same thread.
+
: 释放锁。 该锁必须更早获得,但不一定由同一线程获得。
  
; <span id="_thread.lock.locked"></span><code>lock.</code><code>locked</code><span class="sig-paren">(</span><span class="sig-paren">)</span>
+
; <span id="_thread.lock.locked"></span><span class="sig-prename descclassname"><span class="pre">lock.</span></span><span class="sig-name descname"><span class="pre">locked</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span>
: Return the status of the lock: <code>True</code> if it has been acquired by some thread, <code>False</code> if not.
+
: 返回锁的状态:<code>True</code> 如果它已被某个线程获取,则 <code>False</code> 否则。
  
In addition to these methods, lock objects can also be used via the
+
除了这些方法之外,还可以通过 [[../../reference/compound_stmts#with|with]] 语句使用锁定对象,例如:
[[../../reference/compound_stmts#with|<code>with</code>]] statement, e.g.:
 
  
 
<div class="highlight-python3 notranslate">
 
<div class="highlight-python3 notranslate">
第149行: 第108行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>import _thread
+
<syntaxhighlight lang="python3">import _thread
  
 
a_lock = _thread.allocate_lock()
 
a_lock = _thread.allocate_lock()
  
 
with a_lock:
 
with a_lock:
     print(&quot;a_lock is locked while this executes&quot;)</pre>
+
     print("a_lock is locked while this executes")</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
'''Caveats:'''
+
'''注意事项:'''
  
 
<blockquote><div>
 
<blockquote><div>
第166行: 第125行:
  
 
</div></blockquote>
 
</div></blockquote>
* Threads interact strangely with interrupts: the [[../exceptions#KeyboardInterrupt|<code>KeyboardInterrupt</code>]] exception will be received by an arbitrary thread. (When the [[../signal#module-signal|<code>signal</code>]] module is available, interrupts always go to the main thread.)
+
* 线程与中断进行了奇怪的交互:[[../exceptions#KeyboardInterrupt|KeyboardInterrupt]] 异常将被任意线程接收。 (当 [[../signal#module-signal|signal]] 模块可用时,中断总是转到主线程。)
* Calling [[../sys#sys|<code>sys.exit()</code>]] or raising the [[../exceptions#SystemExit|<code>SystemExit</code>]] exception is equivalent to calling [[#thread.exit|<code>_thread.exit()</code>]].
+
* 调用 [[../sys#sys|sys.exit()]] 或引发 [[../exceptions#SystemExit|SystemExit]] 异常相当于调用 [[#thread.exit|_thread.exit()]]
* It is not possible to interrupt the <code>acquire()</code> method on a lock --- the [[../exceptions#KeyboardInterrupt|<code>KeyboardInterrupt</code>]] exception will happen after the lock has been acquired.
+
* 不可能在锁上中断 <code>acquire()</code> 方法——[[../exceptions#KeyboardInterrupt|KeyboardInterrupt]] 异常将在获取锁后发生。
* When the main thread exits, it is system defined whether the other threads survive. On most systems, they are killed without executing [[../../reference/compound_stmts#try|<code>try</code>]] ... [[../../reference/compound_stmts#finally|<code>finally</code>]] clauses or executing object destructors.
+
* 当主线程退出时,其他线程是否存活由系统定义。 在大多数系统上,它们在不执行 [[../../reference/compound_stmts#try|try]] ... [[../../reference/compound_stmts#finally|finally]] 子句或执行对象析构函数的情况下被杀死。
* When the main thread exits, it does not do any of its usual cleanup (except that [[../../reference/compound_stmts#try|<code>try</code>]] ... [[../../reference/compound_stmts#finally|<code>finally</code>]] clauses are honored), and the standard I/O files are not flushed.
+
* 当主线程退出时,它不会执行任何通常的清理(除了 [[../../reference/compound_stmts#try|try]] ... [[../../reference/compound_stmts#finally|finally]] 子句被遵守),并且标准 I/O 文件不会被刷新。
 +
 
 +
 
 +
</div>
 +
<div class="clearer">
 +
 
  
  
 
</div>
 
</div>
  
[[Category:Python 3.9 中文文档]]
+
[[Category:Python 3.9 文档]]

2021年10月31日 (日) 04:51的最新版本

_thread — 低级线程 API


该模块提供用于处理多线程(也称为 轻量级进程任务 )的低级原语——多个控制线程共享其全局数据空间。 对于同步,提供了简单的锁(也称为 互斥锁二进制信号量 )。 threading 模块提供了一个更易于使用的高级线程 API,该 API 构建在该模块之上。

3.7 版本变更: 该模块以前是可选的,现在一直可用。


该模块定义了以下常量和函数:

exception _thread.error

引发线程特定的错误。

3.3 版更改: 现在是内置 RuntimeError 的同义词。

_thread.LockType
这是锁对象的类型。
_thread.start_new_thread(function, args[, kwargs])

启动一个新线程并返回其标识符。 线程使用参数列表 args(必须是元组)执行函数 function。 可选的 kwargs 参数指定关键字参数的字典。

当函数返回时,线程默默地退出。

当函数因未处理的异常而终止时,会调用 sys.unraisablehook() 来处理异常。 钩子参数的 object 属性是 function。 默认情况下,打印堆栈跟踪,然后线程退出(但其他线程继续运行)。

当函数引发 SystemExit 异常时,它会被静默忽略。

3.8 版更改: sys.unraisablehook() 现在用于处理未处理的异常。

_thread.interrupt_main()

模拟一个 signal.SIGINT 信号到达主线程的效果。 一个线程可以使用这个函数来中断主线程。

如果 signal.SIGINT 没有被 Python 处理(它被设置为 signal.SIG_DFLsignal.SIG_IGN),这个函数什么都不做。

_thread.exit()
引发 SystemExit 异常。 如果没有被捕获,这将导致线程静默退出。
_thread.allocate_lock()
返回一个新的锁对象。 下面描述了锁的方法。 锁最初是解锁的。
_thread.get_ident()
返回当前线程的“线程标识符”。 这是一个非零整数。 它的价值没有直接的意义; 它旨在用作魔术饼干,例如 索引线程特定数据的字典。 当一个线程退出并创建另一个线程时,线程标识符可能会被回收。
_thread.get_native_id()

返回内核分配的当前线程的本机整数线程 ID。 这是一个非负整数。 它的值可用于在系统范围内唯一标识此特定线程(直到线程终止,之后该值可由操作系统回收)。

3.8 版中的新功能。

_thread.stack_size([size])
返回创建新线程时使用的线程堆栈大小。 可选的 size 参数指定用于后续创建的线程的堆栈大小,并且必须为 0(使用平台或配置的默认值)或至少为 32,768 (32 KiB) 的正整数值。 如果未指定 size,则使用 0。 如果不支持更改线程堆栈大小,则会引发 RuntimeError。 如果指定的堆栈大小无效,则会引发 ValueError 并且堆栈大小未修改。 32 KiB 目前是支持的最小堆栈大小值,以保证解释器本身有足够的堆栈空间。 请注意,某些平台可能对堆栈大小的值有特定限制,例如要求最小堆栈大小 > 32 KiB 或要求以系统内存页面大小的倍数进行分配 - 应参考平台文档以获取更多信息(4 KiB 页很常见;如果没有更具体的信息,建议使用 4096 的倍数作为堆栈大小)。
_thread.TIMEOUT_MAX

Lock.acquire()timeout 参数允许的最大值。 指定大于此值的超时将引发 OverflowError

3.2 版中的新功能。

锁对象有以下方法:

lock.acquire(waitflag=1, timeout=- 1)

在没有任何可选参数的情况下,此方法无条件地获取锁,如有必要,等待它被另一个线程释放(一次只有一个线程可以获取锁——这就是它们存在的原因)。

如果存在整数 waitflag 参数,则操作取决于其值:如果为零,则仅在无需等待即可立即获取时才获取锁,如果不为零,则获取锁无条件如上。

如果浮点 timeout 参数存在且为正,则它指定返回前的最大等待时间(以秒为单位)。 负 timeout 参数指定无限等待。 如果 waitflag 为零,则不能指定 timeout

获取锁成功返回值为True,否则返回值为False

3.2 版更改: timeout 参数是新的。

3.2 版更改: 锁定获取现在可以被 POSIX 上的信号中断。

lock.release()
释放锁。 该锁必须更早获得,但不一定由同一线程获得。
lock.locked()
返回锁的状态:True 如果它已被某个线程获取,则 False 否则。

除了这些方法之外,还可以通过 with 语句使用锁定对象,例如:

import _thread

a_lock = _thread.allocate_lock()

with a_lock:
    print("a_lock is locked while this executes")

注意事项:


  • 线程与中断进行了奇怪的交互:KeyboardInterrupt 异常将被任意线程接收。 (当 signal 模块可用时,中断总是转到主线程。)
  • 调用 sys.exit() 或引发 SystemExit 异常相当于调用 _thread.exit()
  • 不可能在锁上中断 acquire() 方法——KeyboardInterrupt 异常将在获取锁后发生。
  • 当主线程退出时,其他线程是否存活由系统定义。 在大多数系统上,它们在不执行 try ... finally 子句或执行对象析构函数的情况下被杀死。
  • 当主线程退出时,它不会执行任何通常的清理(除了 try ... finally 子句被遵守),并且标准 I/O 文件不会被刷新。