本文整理汇总了Python中kazoo.exceptions.LockTimeout方法的典型用法代码示例。如果您正苦于以下问题:Python exceptions.LockTimeout方法的具体用法?Python exceptions.LockTimeout怎么用?Python exceptions.LockTimeout使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类kazoo.exceptions
的用法示例。
在下文中一共展示了exceptions.LockTimeout方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: bounce_lock_zookeeper
# 需要导入模块: from kazoo import exceptions [as 别名]
# 或者: from kazoo.exceptions import LockTimeout [as 别名]
def bounce_lock_zookeeper(
name: str, system_paasta_config: Optional[SystemPaastaConfig] = None
) -> Iterator:
"""Acquire a bounce lock in zookeeper for the name given. The name should
generally be the service namespace being bounced.
This is a contextmanager. Please use it via 'with bounce_lock(name):'.
:param name: The lock name to acquire"""
if system_paasta_config is None:
system_paasta_config = load_system_paasta_config()
zk = KazooClient(
hosts=system_paasta_config.get_zk_hosts(), timeout=ZK_LOCK_CONNECT_TIMEOUT_S,
)
zk.start()
lock = zk.Lock(f"{ZK_LOCK_PATH}/{name}")
try:
lock.acquire(timeout=1) # timeout=0 throws some other strange exception
yield
except LockTimeout:
raise LockHeldException("Service %s is already being bounced!" % name)
else:
lock.release()
finally:
zk.stop()
zk.close()
示例2: _get_lock
# 需要导入模块: from kazoo import exceptions [as 别名]
# 或者: from kazoo.exceptions import LockTimeout [as 别名]
def _get_lock(self):
"""
A wrapper around the Kazoo library's lock. On successful acquisition of the lock, sets self._lock and
self._locked
Returns:
None
"""
logger = self._logger
logger.info(u'Creating lock for {}'.format(str(self)))
try:
lock = self._context.zookeeper_client.Lock(self._path, self._identifier)
except:
logger.exception(u'Failed to create lock object')
return
tries = 0
while (self._retries == 0) or (tries < self._retries):
tries += 1
logger.info(u'Trying to acquire lock for {}. Other contenders: {}'.format(str(self), lock.contenders()))
try:
lock.acquire(timeout=self._timeout)
except LockTimeout:
logger.info(u'Timed out after {} seconds trying to acquire lock for {}'.format(self._timeout,
str(self)))
except Exception as e:
logger.info(u'Error in acquiring lock for {}: {}'.format(str(self), repr(e)))
if lock.is_acquired:
break
if not lock.is_acquired:
logger.warn(u'Failed to acquire lock for {} after {} tries'.format(str(self), tries))
else:
logger.info(u'Lock acquired for {}. Other contenders: {}'.format(str(self), lock.contenders()))
self._locked = True
self._context.zookeeper_client.add_listener(self._lock_listener)
self._lock = lock
示例3: acquire_loop
# 需要导入模块: from kazoo import exceptions [as 别名]
# 或者: from kazoo.exceptions import LockTimeout [as 别名]
def acquire_loop(self, timeout=None):
if timeout is None:
timeout = self.timeout
expire_at = time.time() + timeout
while True:
# Even if timeout is smaller than 0, try-loop continue on until
# maybe_available is not ready.
#
# There is a chance that:
# - Failed to create lock node(lock is occupied by other)
# - Failed to get lock node(just deleted)
# - Failed to create lock node(lock is occupied by other)
# - Failed to get lock node(just deleted)
# - ...
if not self.maybe_available.wait(timeout=expire_at - time.time()):
logger.debug('lock is still held by others: ' + str(self))
if time.time() > expire_at:
raise LockTimeout('lock: ' + str(self.lock_path))
# Always proceed the "get" phase, in order to add a watch handler.
# To watch node change event.
self._create()
self._acquire_by_get()
if self.is_locked():
return
# If it is possible to acquire the lock in next retry, do not yield
if self.maybe_available.is_set():
continue
else:
yield self.lock_holder[0], self.lock_holder[1]
示例4: try_acquire
# 需要导入模块: from kazoo import exceptions [as 别名]
# 或者: from kazoo.exceptions import LockTimeout [as 别名]
def try_acquire(self):
try:
self.acquire(timeout=-1)
except LockTimeout:
pass
# if_locked, lock holder identifier, holder version
return self.is_locked(), self.lock_holder[0], self.lock_holder[1]
示例5: __enter__
# 需要导入模块: from kazoo import exceptions [as 别名]
# 或者: from kazoo.exceptions import LockTimeout [as 别名]
def __enter__(self):
try:
self.lock.acquire(timeout=self.timeout)
except LockTimeout:
log.warning("Already one instance running against this source! exit. See y/oneandonly for help.")
self.close()
sys.exit(1)
示例6: bad_lock
# 需要导入模块: from kazoo import exceptions [as 别名]
# 或者: from kazoo.exceptions import LockTimeout [as 别名]
def bad_lock(self):
bad_lock = mock.Mock()
bad_lock.acquire = mock.Mock(side_effect=LockTimeout('Test exception'))
return bad_lock
示例7: zk_lock
# 需要导入模块: from kazoo import exceptions [as 别名]
# 或者: from kazoo.exceptions import LockTimeout [as 别名]
def zk_lock(zk: KazooClient, lock_path: str, contender_id: str,
timeout: int) -> Generator:
"""
This contextmanager takes a ZooKeeper lock, yields, then releases the lock.
This lock behaves like an interprocess mutex lock.
ZooKeeper allows one to read values without holding a lock, but there is no
guarantee that you will read the latest value. To read the latest value,
you must call `sync()` on a ZNode before calling `get()`.
Args:
zk:
The client to use to communicate with ZooKeeper.
lock_path:
The ZNode path to use as prefix for the locking recipe.
contender_id:
The contender id to identify the current client
in the locking recipe.
timeout:
Time in seconds to wait for the lock to be acquired.
If this time elapses before the lock is acquired, a
`kazoo.exceptions.LockTimeout` exception is raised.
Raises:
kazoo.exceptions.LockTimeout:
If the `timeout` is exceeded without the lock being acquired.
"""
lock = zk.Lock(lock_path, contender_id)
try:
log.info("Acquiring ZooKeeper lock.")
lock.acquire(blocking=True, timeout=timeout, ephemeral=True)
except (ConnectionLoss, SessionExpiredError) as e:
msg_fmt = "Failed to acquire lock: {}"
msg = msg_fmt.format(e.__class__.__name__)
log.exception(msg)
raise e
except LockTimeout as e:
msg_fmt = "Failed to acquire lock in `{}` seconds"
msg = msg_fmt.format(timeout)
log.exception(msg)
raise e
else:
log.info("ZooKeeper lock acquired.")
try:
yield
finally:
log.info("Releasing ZooKeeper lock")
lock.release()
log.info("ZooKeeper lock released.")