functools.lru_cache and multiprocessing: cross-process cache coherence
0 reputation · 05 Feb 2023, 00:51 UTC
Integration boundary
functools.lru_cache and the multiprocessing module are both standard-library components, yet the documentation does not specify how cached results behave when the decorated function runs in multiple worker processes. Each worker receives an independent cache dictionary, so updates to shared state are not reflected in other processes' caches.
Goal and constraints
The objective is to understand whether a single logical cache can be maintained across a multiprocessing.Pool without introducing an external service such as Redis. Constraints include staying within the standard library, preserving the lru_cache API (maxsize, typed), and avoiding a global interpreter lock bottleneck.
Open questions
- Can
multiprocessing.Managerprovide a shared dict that satisfieslru_cache's internal expectations without deadlocks or excessive contention? - Is there a supported pattern for invalidating or updating cache entries across processes when the underlying data changes?
- What are the memory implications of per-process caches when the worker count scales?