发布于 2012-09-25 23:28:13
我最终使用memcached解决了这个问题,因为它有原子更新。
解决方案相当简单:
import memcache
mc = memcache.Client(hosts, debug=1)
# to lock:
def request_lock(key, timeout=300):
if mc == None:
raise Exception("Memcache is not connected!")
# just store a "1" in that location - this is an atomic operation so if two
# nodes request a lock at the same time, only one of them will get a True
return mc.add(key, "1", timeout)
# to unlock:
def request_unlock(key):
if mc == None:
raise Exception("Memcache is not connected!")
return mc.delete(key) > 0https://stackoverflow.com/questions/12078944
复制相似问题