Created
April 10, 2013 01:04
-
-
Save JustinAzoff/5350879 to your computer and use it in GitHub Desktop.
test of dogpile.cache async_creation_runner behavior
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import random | |
import threading | |
from dogpile.cache import make_region | |
from dogpile.cache.api import NO_VALUE | |
def async_creation_runner(cache, somekey, creator, mutex): | |
''' Used by dogpile.core:Lock when appropriate ''' | |
def runner(): | |
try: | |
value = creator() | |
cache.set(somekey, value) | |
finally: | |
mutex.release() | |
thread = threading.Thread(target=runner) | |
thread.start() | |
region = make_region( | |
async_creation_runner=async_creation_runner, | |
).configure( | |
"dogpile.cache.memory", | |
expiration_time = 1 | |
) | |
first = True | |
@region.cache_on_arguments() | |
def test(): | |
global first #hack to make sure it works the first time | |
if not first and random.randint(1,10) < 8: | |
#cloud is stormy | |
raise Exception("foo") | |
first = False | |
return time.ctime() | |
while True: | |
print test() | |
time.sleep(.4) | |
########### | |
def another_way_of_doing_this(func,*args): | |
key = region.function_key_generator('',func)(*args) | |
creator = lambda: func(*args) | |
try : | |
return region.get_or_create(key, creator) | |
except Exception, e: | |
val = region.get(key, ignore_expiration=True) | |
if val is NO_VALUE: | |
raise(e) | |
return val | |
#_test = test | |
#test = lambda: another_way_of_doing_this(_test) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment