本文整理汇总了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
示例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