Dogpile lock class.
Provides an interface around an arbitrary mutex that allows one thread/process to be elected as the creator of a new value, while other threads/processes continue to return the previous version of that value.
Acquire the lock, returning a context manager.
The last known ‘creation time’ of the value, stored as an epoch (i.e. from time.time()).
If the value here is -1, it is assumed the value should recreate immediately.
Return true if the creation function has proceeded at least once.
Return true if the expiration time is reached, or no value is available.
An exception that when raised in the ‘with’ block, forces the ‘has_value’ flag to False and incurs a regeneration of the value.
Generates and return an object, keeping it as a singleton for a certain identifier for as long as its strongly referenced.
class MyFoo(object): "some important object." def __init__(self, identifier): self.identifier = identifier registry = NameRegistry(MyFoo) # thread 1: my_foo = registry.get("foo1") # thread 2 my_foo = registry.get("foo1")
Above, my_foo in both thread #1 and #2 will be the same object. The constructor for MyFoo will be called once, passing the identifier foo1 as the argument.
When thread 1 and thread 2 both complete or otherwise delete references to my_foo, the object is removed from the NameRegistry as a result of Python garbage collection.
NameRegistry is a utility object that is used to maintain new Dogpile objects against a certain key, for as long as that particular key is referenced within the application. An application can deal with an arbitrary number of keys, ensuring that all threads requesting a certain key use the same Dogpile object, without the need to maintain each Dogpile object persistently in memory.
|Parameters:||creator – A function that will create a new value, given the identifier passed to the NameRegistry.get() method.|
Get and possibly create the value.
A mutex which allows multiple readers, single writer.
ReadWriteMutex uses a Python threading.Condition to provide this functionality across threads within a process.
The Beaker package also contained a file-lock based version of this concept, so that readers/writers could be synchronized across processes with a common filesystem. A future Dogpile release may include this additional class at some point.
Acquire the ‘read’ lock.
Acquire the ‘write’ lock.
Release the ‘read’ lock.
Release the ‘write’ lock.