Created
April 24, 2012 01:17
-
-
Save dcramer/2475266 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class Lock(object): | |
""" | |
Uses the defined cache backend to create a lock. | |
>>> with Lock('key name'): | |
>>> # do something | |
""" | |
def __init__(self, lock_key, timeout=10, cache=None): | |
if cache is None: | |
self.cache = _cache | |
else: | |
self.cache = cache | |
self.timeout = timeout | |
self.lock_key = lock_key | |
def __enter__(self): | |
start = time.time() | |
lock_key = self.lock_key | |
cache = self.cache | |
delay = 0.1 | |
attempt = 0 | |
max_attempts = self.timeout / delay | |
got_lock = None | |
while not got_lock and attempt < max_attempts: | |
got_lock = cache.add(lock_key, '', self.timeout) | |
if not got_lock: | |
time.sleep(delay) | |
attempt += 1 | |
if not got_lock: | |
raise UnableToGetLock('Unable to fetch lock after %.2fs' % (time.time() - start,)) | |
return self | |
def __exit__(self, exc_type, exc_value, traceback): | |
try: | |
self.cache.delete(self.lock_key) | |
except Exception, e: | |
logging.exception(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment