开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
python3.8.1
/
Doc
/
library
/
_thread.rst
python3.8.1
/
Doc
/
library
/
_thread.rst
_thread.rst 7.39 KB
一键复制 编辑 原始数据 按行查看 历史
zhangweibo 提交于 2021年11月16日 09:46 +08:00 . git init

:mod:`_thread` --- Low-level threading API

.. module:: _thread
 :synopsis: Low-level threading API.

.. index::
 single: light-weight processes
 single: processes, light-weight
 single: binary semaphores
 single: semaphores, binary


This module provides low-level primitives for working with multiple threads (also called :dfn:`light-weight processes` or :dfn:`tasks`) --- multiple threads of control sharing their global data space. For synchronization, simple locks (also called :dfn:`mutexes` or :dfn:`binary semaphores`) are provided. The :mod:`threading` module provides an easier to use and higher-level threading API built on top of this module.

.. index::
 single: pthreads
 pair: threads; POSIX

.. versionchanged:: 3.7
 This module used to be optional, it is now always available.

This module defines the following constants and functions:

.. exception:: error

 Raised on thread-specific errors.

 .. versionchanged:: 3.3
 This is now a synonym of the built-in :exc:`RuntimeError`.


.. data:: LockType

 This is the type of lock objects.


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

 Start a new thread and return its identifier. The thread executes the
 function *function* with the argument list *args* (which must be a tuple).
 The optional *kwargs* argument specifies a dictionary of keyword arguments.

 When the function returns, the thread silently exits.

 When the function terminates with an unhandled exception,
 :func:`sys.unraisablehook` 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).

 When the function raises a :exc:`SystemExit` exception, it is silently
 ignored.

 .. versionchanged:: 3.8
 :func:`sys.unraisablehook` is now used to handle unhandled exceptions.


.. function:: interrupt_main()

 Simulate the effect of a :data:`signal.SIGINT` signal arriving in the main
 thread. A thread can use this function to interrupt the main thread.

 If :data:`signal.SIGINT` isn't handled by Python (it was set to
 :data:`signal.SIG_DFL` or :data:`signal.SIG_IGN`), this function does
 nothing.


.. function:: exit()

 Raise the :exc:`SystemExit` exception. When not caught, this will cause the
 thread to exit silently.

.. function:: allocate_lock()

 Return a new lock object. Methods of locks are described below. The lock is
 initially unlocked.


.. function:: get_ident()

 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.


.. function:: get_native_id()

 Return the native integral Thread ID of the current thread assigned by the kernel.
 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).

 .. availability:: Windows, FreeBSD, Linux, macOS, OpenBSD, NetBSD, AIX.

 .. versionadded:: 3.8


.. function:: stack_size([size])

 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 :exc:`RuntimeError` is raised. If the specified stack size is
 invalid, a :exc:`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.


.. data:: TIMEOUT_MAX

 The maximum value allowed for the *timeout* parameter of
 :meth:`Lock.acquire`. Specifying a timeout greater than this value will
 raise an :exc:`OverflowError`.

 .. versionadded:: 3.2


Lock objects have the following methods:

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

 Without any optional argument, this method acquires the lock unconditionally, if
 necessary waiting until it is released by another thread (only one thread at a
 time can acquire a lock --- that's their reason for existence).

 If the integer *waitflag* argument is present, the action depends on its
 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.

 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.

 The return value is ``True`` if the lock is acquired successfully,
 ``False`` if not.

 .. versionchanged:: 3.2
 The *timeout* parameter is new.

 .. versionchanged:: 3.2
 Lock acquires can now be interrupted by signals on POSIX.


.. method:: lock.release()

 Releases the lock. The lock must have been acquired earlier, but not
 necessarily by the same thread.


.. method:: lock.locked()

 Return the status of the lock: ``True`` if it has been acquired by some thread,
 ``False`` if not.

In addition to these methods, lock objects can also be used via the :keyword:`with` statement, e.g.:

import _thread

a_lock = _thread.allocate_lock()

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

Caveats:

.. index:: module: signal

Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/python_sourcecode/python3.8.1.git
git@gitee.com:python_sourcecode/python3.8.1.git
python_sourcecode
python3.8.1
python3.8.1
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /