resource-singleflight
A Python library that runs at most one worker per key and hands every caller the same joinable Future.
한국어 · Python 3.9+
Install · Quick start · Why · Reference
50 threads asking for the same key produced 1 work call in 4.6 ms, and a cached submit costs 3.3 µs. Both values are saved in docs/bench/coalesce.json.
A refresh loop has a familiar problem: ten panes wake at the same second and each starts the same expensive read. This coordinator lets the first one work and gives the other nine the same Future to wait on. When a read has to be replaced, the old one keeps running but cannot publish its result any more, so a slow answer never overwrites a fresh one.
Standard library only. One file, no threads of its own beyond the workers it starts.
Install
pip install resource-singleflight
Python 3.9+. No dependencies. Verified on macOS arm64 with Python 3.12.
Quick start
from resource_singleflight import ResourceCoordinator
coordinator = ResourceCoordinator(thread_name_prefix="refresh")
def read_expensive() -> str:
return "value"
first = coordinator.submit("key", read_expensive, cache_ttl=30.0)
second = coordinator.submit("key", read_expensive, cache_ttl=30.0)
print(first is second) # True: one worker, one Future
print(first.result(1)) # 'value'
Ten callers, one work call. The work function blocks until every caller has asked, which is the case coalescing is for:
import threading
from resource_singleflight import ResourceCoordinator
coordinator = ResourceCoordinator()
started, release = threading.Event(), threading.Event()
calls = []
def read():
calls.append(1)
started.set()
release.wait(5)
return "value"
futures = []
target = lambda: futures.append(coordinator.submit("key", read, cache_ttl=30.0))
threads = [threading.Thread(target=target) for _ in range(10)]
for thread in threads:
thread.start()
started.wait(2)
release.set()
for thread in threads:
thread.join(5)
print(len(calls)) # 1
print({f.result(2) for f in futures}) # {'value'}
A forced refresh replaces the in-flight read, and the superseded Future resolves from the replacement instead of hanging:
old = coordinator.submit("key", slow_read, cache_ttl=30.0)
new = coordinator.submit("key", fast_read, cache_ttl=30.0, force=True)
print(old.result(1)) # the replacement's result, not the stale one
Why
Coalescing requests sounds like a lock around a dictionary. The parts that bite are the edges:
- A detached read must not publish. If a refresh was replaced, its result is no longer wanted. The coordinator bumps a generation when it replaces work, and a worker may only publish while its generation is current. A superseded worker that finishes late resolves nothing and caches nothing.
- Nobody should hang. Cancelling a
Futuresomeone is waiting on would raiseCancelledErrorin a caller that did nothing wrong. Instead the issuedFutureis resolved from the replacement, so every caller gets an answer. - Work in progress stays owned. Arbitrary Python code already inside a syscall cannot be stopped. The coordinator keeps every started worker until it exits, and
wait_for_idle()reports when the last one is done. A shutdown can then wait instead of hoping. - Cache and publication agree. The generation check and the cache write happen under the same lock, so a replacement lands either wholly before a publication or wholly after it.
Reference
ResourceCoordinator(thread_name_prefix="singleflight")
| Method | Behaviour |
|---|---|
submit(key, function, *, cache_ttl=0.0, force=False) |
Joins the in-flight Future for key, or serves a cached value, or starts one worker. force=True replaces the current generation. |
invalidate(key) |
Drops the cached value and retires the joinable flight, retaining ownership of any running worker. |
clear() |
Drops every cached value and cancels every joinable flight, keeping ownership of running workers. |
wait_for_idle(timeout=None) |
Waits until every worker this coordinator started has exited. Returns False on timeout. |
key is any hashable. function takes no arguments; close over what it needs.
env_flag(names, *, default=True)
Reads a boolean switch from the first of names that is set in the environment. Unset keeps default. Any value outside 0, false, no, off keeps it enabled, so a typo never silently disables coordination.
Numbers
Measured on 2026-11-19 on macOS arm64 (M5 Pro), Python 3.12.8, from docs/bench/coalesce.json.
| What | Result |
|---|---|
| 50 threads submitting one key, one 20 ms work item | 1 work call, 4.6 ms total |
| Cached submit, result already available | 3.3 µs per submit |
Limits
- One process. There is no cross-process coordination; two processes each run their own worker.
- Only overlapping submissions coalesce. If the work finishes before the next caller submits, that caller starts its own call, or is served the cache when
cache_ttlis above zero. Ten threads submitting a 5 µs function can produce several calls; the result is still correct. - No persistence. The cache is an in-memory dict with a monotonic deadline per key.
- A worker cannot be interrupted.
forceredirects its result; it does not stop it. Usewait_for_idle()when shutdown must be bounded. - Linux and Windows have not been exercised.
FAQ
Why not functools.lru_cache? A cache has no in-flight state, so callers of a cold key each start their own read. This type also owns replacement, which a plain cache does not express.
Why a Future and not a lock plus a dict? A lock says "wait". A Future carries the result, the exception and the cancellation state, and a caller can choose its own timeout.
Contributing
Bug reports and patches are welcome. See CONTRIBUTING.md for the checks and the fixture each behaviour change needs.
Licence
MIT, in LICENSE.
Metadata
Release files for resource-singleflight 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| resource_singleflight-0.1.0.tar.gz | 10.8 kB | Without provenance | Details for resource_singleflight-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| resource_singleflight-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for resource_singleflight-0.1.0-py3-none-any.whl |
Total release size: 20.0 kB
Release files / resource_singleflight-0.1.0.tar.gz
| Download URL | resource_singleflight-0.1.0.tar.gz |
|---|---|
| Size | 10.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3368870cc08298aaaf1b722446f40063d8aa0bf5dbcaedd688acc6a8389f9311
|
|
BLAKE2b-256 checksum How to use checksums |
f5b2af82d1d1c14356617d6dbf3c2a3239d8db607fed8a1171c4d6cf7160caae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.8
|
Release files / resource_singleflight-0.1.0-py3-none-any.whl
| Download URL | resource_singleflight-0.1.0-py3-none-any.whl |
|---|---|
| Size | 9.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b7555d64c81808f27b4dc7f749fbde3c72c597334ddb08fb3f8ecd56af3b28d6
|
|
BLAKE2b-256 checksum How to use checksums |
07b4de43d9500a5492be2f31ce0b9912537baf1fd3acbaa0d0f26d4495b2a62f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.8
|
Security for resource-singleflight 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.