我的问题与Kazoo / Zookeeper有关,但更笼统地说是关于分配资源,然后让该资源的调用者不暂停。来自Kazoo文档:
强烈建议使用add_listener()添加状态侦听器 并注意“丢失”和“挂起”状态的更改并做出反应 适当地。如果发生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建议您实施connection state listener:
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
答案 0 :(得分:1)
这可能不是最佳设计,但是您可以提供对dut: D_FF
port map (
D => D_s,
s => s_s,
CLOCK => CLOCK_s,
Q => Q_s
);
语句主体锁的引用:
with
针对您的问题(我对Kazoo并不熟悉),您可以在上下文管理器中定义@contextmanager
def acquire_lock(self, path, identifier):
lock = zk.Lock(path, identifier)
try:
retry = KazooRetry()
retry(lock.acquire)
yield lock
finally:
lock.release()
with acquire_lock('/some_op/') as lock:
...
if necessary:
lock.release()
...
,在该位置可以关闭锁。
my_listener
您的上下文可能需要产生监听器,或注册它(或对监听器进行任何操作)本身,但是无论您如何使用它,它都可以访问您在上下文管理器中创建的锁。