Created
April 24, 2013 15:57
-
-
Save josiahcarlson/5453257 to your computer and use it in GitHub Desktop.
Refreshing a lock defined in 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
''' | |
Answer to this thread: | |
http://www.manning-sandbox.com/thread.jspa?messageID=140363 | |
''' | |
def refresh_lock(conn, lockname, identifier, 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, 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
Very useful, thanks! Did you forget the line
pipe.set(lockname, identifier)
?