Skip to content

Instantly share code, notes, and snippets.

@atamurad
Created July 3, 2011 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save atamurad/1062584 to your computer and use it in GitHub Desktop.
Save atamurad/1062584 to your computer and use it in GitHub Desktop.
mutex with redis
def acquire(name):
tries = 0
while True:
tries += 1
if tries >= 30:
raise Exception("Couldn't acquire lock on mutex "+name)
try:
n = r.incr("lock-"+name)
if n == 1:
return True
r.decr("lock-"+name)
time.sleep(1)
print "waiting for release on mutex "+name
# TODO: potential infinite loop..
except Exception as ex:
print ex
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment