Use `dict.setdefault()` in functools caching for better thread-safety
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 35.9k
- PR 合併指標
- PR 指標待擷取
描述
Feature or enhancement
Proposal:
Currently, functools.cache family of functions uses _PyDict_SetItem_KnownHash. But I am wondering if it shouldn't use SetDefault style access at basically no-cost?
That way, when working with many threads, while multiple evaluations are still possible, all threads are guaranteed to end up with the same object.
There may be great reasons why that isn't wanted, but on first sight it seems like a simple and practically free way to remove a pit-fall for threaded code.
(Of course building a custom version that does this is easy, but functools.cache is great and used quite a lot.)
Admittedly, an optional once= would also be a very cool feature (or maybe as a dedicated pattern).
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Linked PRs
- gh-154069
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
先從 issue 中對 functools.cache、_PyDict_SetItem_KnownHash 和 SetDefault 的描述開始,然後檢視連結的 PR gh-154069。在進行任何變更之前,確定預期的執行緒安全性和物件身分語義;只有在設計獲得接受並完成相應的實作和測試後,才能算作完成。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- backend
- Issue 類型
- 功能
- 難度
- 5/5
- 預估耗時
- 一週以上
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 25/100