threading.Semaphore documentation is not internally consistent
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- PR 合併指標
- PR 指標待擷取
描述
Documentation
threading.Semaphore has two methods, acquire() and release(). The documentation for acquire() states:
Exactly one thread will be awoken by each call to release().
The documentation for release() states:
Release a semaphore, incrementing the internal counter by n. When it was zero on entry and other threads are waiting for it to become larger than zero again, wake up n of those threads.
These statements cannot both be true. Which is correct? Technically, I suspect... neither. Though my guess is the latter is far closer to the truth.
The n parameter to release() was added in Python 3.9; before that development, the statement in acquire() would have been true. So I suspect the statement in the documentation for acquire() is simply old information, rendered obsolete by this new parameter.
However, the documentation for release() says it will wake up n threads. But what if fewer than n threads are waiting? It can't wake up threads that don't exist.
Someone should read the implementation of threading.Semaphore, figure out what its behavior actually is, and rewrite the documentation to accurately reflect that behavior. My suspicion is that this statement correctly describes its behavior:
If there are j threads blocked on
acquire(), and you callrelease(n), release will wake upmin(j, n)of those threads.
It might be nice to backport this change to all currently-supported versions, too.
Linked PRs
- gh-141243
- gh-141244
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
先閱讀 threading.Semaphore 的實作,然後比較 issue 中連結的 acquire() 和 release() 文件項目。更新文件,使兩處描述都與實作保持一致,包括等待中的執行緒少於 n 個時的 release(n) 行為,並核對支援版本文件中的措辭。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- documentation
- Issue 類型
- 文件
- 難度
- 3/5
- 預估耗時
- 1-2 天
- 活躍度
- 停滯
- 描述清晰度
- 描述清楚
- 新手友好度
- 25/100