Skip to content

Instantly share code, notes, and snippets.

@chrisglass
Forked from atamurad/gist:1062584
Created July 4, 2011 08:21
Show Gist options
  • Save chrisglass/1063056 to your computer and use it in GitHub Desktop.
Save chrisglass/1063056 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