Clarify the Purpose of the PyGILState API and Identify Better Alternatives
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
This especially applies to the following sections:
- https://docs.python.org/3/c-api/init.html#c.PyGILState_Ensure
- https://docs.python.org/3/c-api/init.html#c.PyGILState_Release
but also:
- https://docs.python.org/3/c-api/init.html#c.PyGILState_GetThisThreadState
- https://docs.python.org/3/c-api/init.html#c.PyGILState_Check
The PyGILState_* C-API was introduced by PEP 311 as a convenience for extension maintainers for some very specific situations:
- code running in the current thread does not know if it holds the GIL
- this situation may involve reentrancy
The PyGILState_* API also (almost incidentally) supports use cases that are more appropriately satisfied by other API, like Py_BEGIN_ALLOW_THREADS()/Py_END_ALLOW_THREADS() for balanced acquire-release.
The documentation isn't so clear about the specific use cases, nor that an interested user may actually want a different API. For example, from a recent issue I saw: We were wrapping each evaluation in PyGILState_Ensure and PyGILState_Release, which I believe is a correct way to do this.
FTR, https://docs.python.org/3/c-api/init.html#non-python-created-threads does a decent job of explaining, as does the PEP, but the entries for PyGILState_Ensure() and PyGILState_Release() are less clear.
FWIW, I expect that it is easier now to deal with the motivating cases using the exiting C-API than it was in 2003. I'm not sure we would accept something like PEP 311 if it were proposed today. Then again, for a pair of operations that needs to balanced like this, having a specific pair of functions is helpful.
Related: a recent discussion about replacing the PyGILState_* API: https://discuss.python.org/t/a-new-api-for-ensuring-releasing-thread-states/83959.
Also see gh-131264 and gh-131265.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
先从 PyGILState_Ensure()、PyGILState_Release()、PyGILState_GetThisThreadState() 和 PyGILState_Check() 的文档条目开始,然后将其与 PEP 311、非 Python 创建的线程部分以及链接的讨论进行比较。完成的标准是这些条目能够清楚地解释其预期用例,并在适当情况下引导读者使用更好的替代方案。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- c, python
- 领域
- documentation
- Issue 类型
- 文档
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 42/100