當前位置: 首頁>>代碼示例>>Python>>正文


Python exceptions.LockTimeout方法代碼示例

本文整理匯總了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() 
開發者ID:Yelp,項目名稱:paasta,代碼行數:26,代碼來源:bounce_lib.py

示例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 
開發者ID:yahoo,項目名稱:panoptes,代碼行數:42,代碼來源:lock.py

示例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] 
開發者ID:bsc-s2,項目名稱:pykit,代碼行數:40,代碼來源:zklock.py

示例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] 
開發者ID:bsc-s2,項目名稱:pykit,代碼行數:11,代碼來源:zklock.py

示例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) 
開發者ID:Yelp,項目名稱:data_pipeline,代碼行數:9,代碼來源:zookeeper.py

示例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 
開發者ID:Yelp,項目名稱:data_pipeline,代碼行數:6,代碼來源:zookeeper_test.py

示例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.") 
開發者ID:dcos,項目名稱:dcos,代碼行數:52,代碼來源:etcd_discovery.py


注:本文中的kazoo.exceptions.LockTimeout方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。