Expose `threading.Semaphore._value`

未关闭
#93,213 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
python

调研方向

从 threading.Semaphore 及其当前的 repr 行为入手,然后比较 asyncio.locks.Semaphore.locked 和 multiprocessing.Semaphore.get_value。确定公共 API 是否应公开该值,还是仅公开其为零的状态,并在为所选接口定义测试和文档之前,考虑现有的讨论和理由。

由索引模型根据 Issue 内容生成。

描述

stdlib type-feature

Feature or enhancement

It would be useful to be able to access the value of threading.Semaphore._value as part of the public API of Semaphore, say via a get_value method.

Pitch

The value is useful for debugging and testing, helping users to understand the state and invariants of their code. There is also educational value in allowing users to see the internal state of a Semaphore, seeing as how it's an important concurrency primitive.

https://cs.github.com/?scopeName=All+repos&scope=&q=semaphore._value+language%3Apython# finds 68 uses of the exact phrase "semaphore._value" on Github, so I'm not alone in wanting access to this (although several of these are asyncio.locks.Semaphore).

Previous discussion

asyncio.locks.Semaphore has a locked method that exposes whether the value of the Semaphore is zero. It does appear that many of the uses are comparing the value to zero, so adding a locked method to threading.Semaphore would be sufficient for a lot of usage.

While writing this issue, I noticed that in 3.11, threading.Semaphore has its value printed in __repr__ (changed in #20534, the bpo mentions "expose their public states" as the rationale)

It looks like multiprocesing.Semaphore has an undocumented get_value method (although there is an issue about potentially removing it https://github.com/python/cpython/issues/84974 )

主要语言
Python
星标
77.2k
派生
36k
平均合并
1 天 9 小时
30 天内合并 PR
558

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

python/cpython 的其他 Issue

查看 python/cpython 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。