hash, so that the string length is fixed. To
disable all key mangling, set to ``False``.
- :param background_runner: A callable that, when specified, will
- be called by dogpile.lock when there is a stale value present
- in the cache. It will be passed the mutex and gen_value callable
- and is responsible for invoking gen_value and releasing the
- mutex when finished. This can be used to defer the computation
- of expensive creator functions to later points in the future by
- way of, for example, a background thread, a long-running queue,
- or a task manager system like Celery.
+ :param async_creator_factory: A callable that, when specified,
+ will be used to produce an async_creator callable for dogpile.lock.
+ That callable, when available, will be called by dogpile.lock when
+ there is a stale value present in the cache. It will be passed the
+ mutex is responsible releasing that mutex when finished.
+ This can be used to defer the computation of expensive creator
+ functions to later points in the future by way of, for example, a
+ background thread, a long-running queue, or a task manager system
- For a specific example
, using background_runner, new values can
+ For a specific example using , new values can
be created in a background thread like so::
- def my_background_runner(mutex, gen_value):
+ def async_creator_factory(cache, somekey, creator):
+ ''' Returns an async_creator callable for use by dogpile.core:Lock '''
- thread = threading.Thread(target=f)
+ def async_creator(mutex):
+ ''' Used by dogpile.core:Lock when appropriate '''
+ cache.set(somekey, value)
+ thread = threading.Thread(target=runner)
- "url": "127.0.0.1:11211",
+ 'url': '127.0.0.1:11211',
+ 'distributed_lock': True,
Remember that the first request for a key with no associated
- value will always block;
background_runner will not be invoked.
+ value will always block; r will not be invoked.
However, subsequent requests for cached-but-expired values will
still return promptly. They will be refreshed by whatever
- asynchronous means the provided
+ asynchronous means the provided r callable
- By default the background_runner is disabled and is set to ``None``.
+ By default the async_creator_factory is disabled and is set
"""Construct a new :class:`.CacheRegion`."""
self.function_key_generator = function_key_generator
background_runner = background_runner
def configure(self, backend,
if expiration_time is None:
expiration_time = self.expiration_time
+ if self.async_creator_factory:
+ async_creator = self.async_creator_factory(self, key, creator)
self.background_runner) as value:
+ r) as value:
def decorate(*arg, **kw):
key = key_generator(*arg, **kw)
return self.get_or_create(key, creator, expiration_time)