Created
July 3, 2011 20:41
-
-
Save atamurad/1062584 to your computer and use it in GitHub Desktop.
mutex with redis
This file contains hidden or 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
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