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


Python BrokerConnection.ensure_connection方法代码示例

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


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

示例1: Worker

# 需要导入模块: from kombu import BrokerConnection [as 别名]
# 或者: from kombu.BrokerConnection import ensure_connection [as 别名]
class Worker(ConsumerMixin):
    def __init__(self, **kwargs):
        self.__callbacks = kwargs.get('callbacks',self.on_message)
        self.__amqp_url = kwargs.get('amqp_url',AMQP_URL)
        self.__queue = kwargs.get('queue')
        self.__conn_retries = kwargs.get('max_retries',2)
        if self.__queue is None:
            raise TypeError('invalid worker queue parameter')
        self.connection = BrokerConnection(self.__amqp_url)
        self.connection.ensure_connection(max_retries=self.__conn_retries, callback=self.on_conn_retry)
    
    def get_consumers(self, consumer, channel):
        if not isinstance(self.__callbacks,list):
            self.__callbacks = [ self.__callbacks ]
        return [consumer(self.__queue, callbacks=self.__callbacks)]

    def on_message(self, body, message):
        out =  'Received message: %r' % dumps(body)
        out += ' properties: %s' % dumps(message.properties)
        out += '  delivery_info: %s' % dumps(message.delivery_info)
        LOG.info(out,json=True)
        message.ack()

    def on_conn_retry(self):
        LOG.error('Retrying connection for {0}'.format(self.__amqp_url))

    def start(self):
        LOG.info('Starting AMQP worker {0}'.format(self.__queue))
        self.run()

    def stop(self):
        LOG.info('Stopping AMQP worker {0}'.format(self.__queue))
        self.should_stop = True        
开发者ID:logtraynor,项目名称:RackHD,代码行数:35,代码来源:amqp.py

示例2: AMQPWorker

# 需要导入模块: from kombu import BrokerConnection [as 别名]
# 或者: from kombu.BrokerConnection import ensure_connection [as 别名]
class AMQPWorker(ConsumerMixin):
    def __init__(self, **kwargs):
        self.__callbacks = kwargs.get('callbacks',self.on_message)
        self.__amqp_url = kwargs.get('amqp_url',AMQP_URL)
        self.__queue = kwargs.get('queue')
        self.__max_retries = kwargs.get('max_retries',2)
        self.__max_error = kwargs.get('max_error',3)
        if self.__queue is None:
            raise TypeError('invalid worker queue parameter')
        self.connection = BrokerConnection(self.__amqp_url)
        self.connection.ensure_connection(max_retries=self.__max_retries, 
                errback=self.on_connection_error, callback=self.on_conn_retry)
    
    def get_consumers(self, consumer, channel):
        if not isinstance(self.__callbacks,list):
            self.__callbacks = [ self.__callbacks ]
        return [consumer(self.__queue, callbacks=self.__callbacks)]

    def on_message(self, body, message):
        out = {
            'message':body, 
            'properties':message.properties, 
            'delivery_info': message.delivery_info
        }
        LOG.info(out, json=True)
        message.ack()

    def on_conn_retry(self):
        LOG.error('Retrying connection for {0}'.format(self.__amqp_url))

    def on_connection_error(self, exc, interval):
        if self.__max_error:
            LOG.warning('Connection error, retrying in {0} seconds (retry={1})'.format(interval, self.__max_error))
            self.__max_error -= 1
        else:
            LOG.error('max connection errors exceeded.')
            stop()

    def start(self):
        LOG.info('Starting AMQP worker {0}'.format(self.__queue))
        self.should_stop = False
        self.run()

    def stop(self):
        LOG.info('Stopping AMQP worker {0}'.format(self.__queue))
        self.should_stop = True        
开发者ID:BillyAbildgaard,项目名称:RackHD,代码行数:48,代码来源:amqp.py


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