17.9. _thread — 低级线程 API

此模块提供用于处理多个线程的低级原语 (也称 轻量进程 or tasks ) — 多个控制线程共享其全局数据空间。对于同步而言,简单锁 (也称 mutexes or 二进制信号量 ) 被提供。 threading 模块提供构建于此模块之上,更易于使用且更高级的线程 API。

The module is optional. It is supported on Windows, Linux, SGI IRIX, Solaris 2.x, as well as on systems that have a POSIX thread (a.k.a. “pthread”) implementation. For systems lacking the _thread module, the _dummy_thread module is available. It duplicates this module’s interface and can be used as a drop-in replacement.

It defines the following constants and functions:

exception _thread. error

引发特定线程错误。

3.3 版改变: 这现在是同义词内置 RuntimeError .

_thread. LockType

这是锁对象的类型。

_thread. start_new_thread ( function , args [ , kwargs ] )

启动新线程并返回其标识符。线程执行函数 function 采用自变量列表 args (其必须是元组)。可选 kwargs argument specifies a dictionary of keyword arguments. When the function returns, the thread silently exits. When the function terminates with an unhandled exception, a stack trace is printed and then the thread exits (but other threads continue to run).

_thread. interrupt_main ( )

引发 KeyboardInterrupt exception in the main thread. A subthread can use this function to interrupt the main thread.

_thread. exit ( )

引发 SystemExit 异常。若未被捕获,这将导致线程默默退出。

_thread. allocate_lock ( )

返回新的锁对象。锁方法的描述如下。锁最初是解锁的。

_thread. get_ident ( )

返回当前线程的 "线程标识符"。这是非 0 整数。它的值没有直接意义;旨在作为魔法 Cookie 使用 (如:索引特定线程数据的字典)。线程标识符会被回收,当退出线程并创建另一线程时。

_thread. stack_size ( [ size ] )

返回创建新线程时使用的线程堆栈大小。可选 size 自变量指定用于随后创建线程的堆栈大小,且必须为 0 (使用平台或配置默认的) 或至少 32,768 (32 KB) 的正整数值。若 size 未指定,使用 0。若不支持改变线程堆栈大小, RuntimeError 被引发。若指定堆栈大小无效, ValueError 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 > 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). Availability: Windows, systems with POSIX threads.

_thread. TIMEOUT_MAX

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

3.2 版新增。

锁对象拥有下列方法:

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

当没有任何可选自变量时,此方法无条件获得锁,若有必要,等待被另一线程释放 (每次仅一线程可以获得锁 — 这是它们存在的原因)。

若整数 waitflag 自变量存在,动作从属其值:若它为 0,才获得锁当不用等待就可以立即获得时,若它为非 0,如上无条件获得锁。

若浮点 timeout 自变量存在且为正值,指定返回前的最大等待时间 (以秒为单位)。负值 timeout 自变量指定无界等待。不可以指定 timeout if waitflag 为 0。

返回值为 True 若成功获取锁, False 若不。

3.2 版改变: The 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 exception is equivalent to calling _thread.exit() .
  • 它是不可能的,中断 acquire() 方法在锁 — KeyboardInterrupt 异常将在已获得锁之后发生。
  • When the main thread exits, it is system defined whether the other threads survive. On most systems, they are killed without executing try ... finally clauses or executing object destructors.
  • When the main thread exits, it does not do any of its usual cleanup (except that try ... finally clauses are honored), and the standard I/O files are not flushed.

上一话题

17.8. dummy_threading — 直接置换 threading 模块

下一话题

17.10. _dummy_thread — 直接置换 _thread 模块

本页