当前位置: 首页>>代码示例>>Python>>正文


Python Service.isConnected方法代码示例

本文整理汇总了Python中cocaine.services.Service.isConnected方法的典型用法代码示例。如果您正苦于以下问题:Python Service.isConnected方法的具体用法?Python Service.isConnected怎么用?Python Service.isConnected使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在cocaine.services.Service的用法示例。


在下文中一共展示了Service.isConnected方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: ReconnectableService

# 需要导入模块: from cocaine.services import Service [as 别名]
# 或者: from cocaine.services.Service import isConnected [as 别名]
class ReconnectableService(object):

    DEFAULT_HOST = 'localhost'
    DEFAULT_PORT = 10053
    DEFAULT_ADDRESS = '{host}:{port}'.format(host=DEFAULT_HOST,
                                             port=DEFAULT_PORT)

    def __init__(self,
                 app_name,
                 addresses=None,
                 attempts=3,
                 delay=0.1, max_delay=60.0, delay_exp=2.0,
                 connect_timeout=None,
                 timeout=None,
                 logger=None):
        self.delay = delay
        self.max_delay = max_delay
        self.delay_exp = delay_exp
        self.connect_timeout = connect_timeout
        self.timeout = timeout
        self.attempts = attempts
        self.logger = logger or Logger()
        self._reset()

        addresses = addresses or ReconnectableService.DEFAULT_ADDRESS
        pairs = []
        for address in addresses.split(','):
            address_parts = address.split(':')
            host = address_parts[0]
            port = (len(address_parts) > 1 and int(address_parts[1]) or
                    ReconnectableService.DEFAULT_PORT)
            pairs.append((host, port))
        self.addresses = itertools.cycle(pairs)

        self.app_name = app_name
        self.upstream = None

    def _reset(self):
        self._cur_delay = self.delay

    @chain.source
    def enqueue(self, handler, data, attempts=None, timeout=None):
        attempt = 1
        request_attempts = attempts or self.attempts
        while True:
            try:
                yield self._reconnect_if_needed()
                yield self.upstream.enqueue(handler, data, timeout=timeout or self.timeout)
                self._reset()
                break
            except Exception as e:
                error_str = 'Upstream service request failed (attempt {}/{}): {}'.format(
                    attempt, request_attempts, e)
                if isinstance(e, CommunicationError):
                    self.logger.error(error_str)
                    if isinstance(e, DisconnectionError):
                        self.logger.debug('Disconnection from upstream service, '
                                          'will reconnect on next attempt')
                        self.upstream = None
                else:
                    self.logger.error(error_str)
                if attempt >= request_attempts:
                    self._reset()
                    raise
                attempt += 1
                yield self._delay()

    @chain.source
    def _delay(self):
        d = Deferred()
        ioloop.IOLoop.current().add_timeout(timedelta(seconds=self._cur_delay),
                                            lambda: d.trigger(None))
        self.logger.debug('Delaying for {:.2f} s'.format(self._cur_delay))
        yield d
        self.logger.debug('Resuming from delay...')
        self._cur_delay = min(self._cur_delay * self.delay_exp, self.max_delay)

    @chain.source
    def _reconnect_if_needed(self):
        if not self.upstream:
            host, port = self.addresses.next()
            self.upstream = Service(self.app_name, blockingConnect=False)
            self.logger.debug('Connecting to upstream service "{}", host={}, '
                              'port={}'.format(self.app_name, host, port))
            yield self.upstream.connect(host=host, port=port,
                                        timeout=self.connect_timeout,
                                        blocking=False)

        if not self.upstream.isConnected():
            try:
                self.logger.debug(
                    'Reconnecting to upstream service "{}"'.format(
                        self.app_name))
                yield self.upstream.reconnect(timeout=self.connect_timeout,
                                              blocking=False)
            except IllegalStateError:
                # seems to be in connecting state
                pass
开发者ID:agodin,项目名称:mastermind,代码行数:100,代码来源:service.py


注:本文中的cocaine.services.Service.isConnected方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。