Skip to content

Instantly share code, notes, and snippets.

@josiahcarlson
Created April 24, 2013 15:57
Show Gist options
  • Save josiahcarlson/5453257 to your computer and use it in GitHub Desktop.
Save josiahcarlson/5453257 to your computer and use it in GitHub Desktop.
Refreshing a lock defined in chapter 6 of Redis in Action
'''
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
@whosken
Copy link

whosken commented Feb 3, 2015

Very useful, thanks! Did you forget the line pipe.set(lockname, identifier)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment