Created
October 15, 2012 05:36
-
-
Save josiahcarlson/3890959 to your computer and use it in GitHub Desktop.
A way to refresh a lock from chapter 6 of Redis in Action
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
'''A function to refresh a lock that can timeout, before it times out.''' | |
def refresh_lock(conn, lockname, identifier, lock_timeout=10): | |
pipe = conn.pipeline(True) | |
lockname = 'lock:' + lockname | |
while True: | |
try: | |
pipe.watch(lockname) | |
if pipe.get(lockname) == identifier: | |
pipe.multi() | |
pipe.expire(lockname, lock_timeout) | |
pipe.execute() | |
return True | |
pipe.unwatch() | |
break | |
except redis.exceptions.WatchError: | |
pass | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment