有没有一个Python等价于Ruby的officer gem?

2024-06-25 06:12:34 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有一个Python等价于Ruby的officergem?它是一个分布式锁服务器,基本上允许您在网络上而不仅仅是在线程之间拥有锁。在

我一直在研究elock,它有一个Python库,但是elock是未经授权的,所以我们不能将它用于商业软件(它或多或少已经被放弃了)。在

理想情况下,等价物将适合twisted,但这不是必需的。在


Tags: 网络服务器分布式twisted情况ruby等价理想
2条回答

如果您所做的是针对非对称项目,请查看列表here。它有一个关于使用python的集群计算的部分,两个最有前途的是celery和{a3}。该页还具有用于在一台计算机上的进程之间拆分任务的资源。它也可以用于对称项目,但我认为锁几乎必须由“服务器”拥有。在

我最后用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) > 0

相关问题 更多 >