Skip to content

Instantly share code, notes, and snippets.

@josiahcarlson
Created October 15, 2012 05:36
Show Gist options
  • Save josiahcarlson/3890959 to your computer and use it in GitHub Desktop.
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
'''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