Use `dict.setdefault()` in functools caching for better thread-safety
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the issue's description of functools.cache, _PyDict_SetItem_KnownHash, and SetDefault, then review linked PR gh-154069. Determine the intended thread-safety and object-identity semantics before changing anything; done would require an accepted design and corresponding implementation and tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100