我的问题与 Kazoo/Zookeeper 有关,但更一般地说,它是关于分配资源,然后让该资源的调用者不应该暂停。来自 Kazoo 文档:
强烈建议使用 add_listener() 添加状态侦听器,并注意 LOST 和 SUSPENDED 状态更改并适当地重新采取行动。如果发生 LOST 状态,则可以确定锁定和/或租约已丢失。
我正在使用上下文管理器来分配锁:
@contextmanager
def lock(self, path, identifier):
lock = zk.Lock(path, identifier)
lock.acquire()
yield lock <--- how to communicate connection loss from client?
finally:
lock.release()
像这样使用:
with lock('/some_op/') as lck:
# do something with lock, but pause in case of connection loss?
如果连接丢失,您如何分配锁但撤销访问?
以下是 Kazoo 建议您实现连接状态侦听器的方式:
def my_listener(state):
if state == KazooState.LOST:
# Register somewhere that the session was lost
elif state == KazooState.SUSPENDED:
# Handle being disconnected from Zookeeper
else:
# Handle being connected/reconnected to Zookeeper