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) > 0
如果您所做的是针对非对称项目,请查看列表here。它有一个关于使用python的集群计算的部分,两个最有前途的是celery和{a3}。该页还具有用于在一台计算机上的进程之间拆分任务的资源。它也可以用于对称项目,但我认为锁几乎必须由“服务器”拥有。在
我最后用memcached来解决这个问题,因为它有原子更新。在
解决方案相当简单:
相关问题 更多 >
编程相关推荐