本文整理汇总了Python中pika.adapters.BlockingConnection.add_backpressure_callback方法的典型用法代码示例。如果您正苦于以下问题:Python BlockingConnection.add_backpressure_callback方法的具体用法?Python BlockingConnection.add_backpressure_callback怎么用?Python BlockingConnection.add_backpressure_callback使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pika.adapters.BlockingConnection
的用法示例。
在下文中一共展示了BlockingConnection.add_backpressure_callback方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: AMQPLink
# 需要导入模块: from pika.adapters import BlockingConnection [as 别名]
# 或者: from pika.adapters.BlockingConnection import add_backpressure_callback [as 别名]
class AMQPLink(object):
exceptions = pika.exceptions
class PikaError(Exception): pass
link = None
tx = True
def __init__( self, host, auth, exchange,
heartbeat=False, reconnect_delays=5,
libc_gethostbyname=None, log=None ):
self.log = log or logging.getLogger('amqp')
if heartbeat: raise NotImplementedError
self.host, self.auth, self.exchange, self.heartbeat,\
self.libc_gethostbyname = host, auth, exchange, heartbeat, libc_gethostbyname
if isinstance(reconnect_delays, (int, float)): reconnect_delays = [reconnect_delays]
self.reconnect_delays, self.reconnect_info = reconnect_delays, None
self.connect() # mainly to notify if it fails at start
def schema_init(self):
exchange = self.exchange.copy()
self.ch.exchange_declare(exchange=exchange.pop('name'), **exchange)
def _error_callback(self, msg, *argz, **kwz):
raise kwz.pop('err', self.PikaError)(msg)
def connect(self):
host = self.host
if self.libc_gethostbyname: host = self.libc_gethostbyname(self.host)
while True:
if self.link and self.link.is_open:
try: self.link.close()
except: pass
try:
self.log.debug('Connecting to AMQP broker ({})'.format(host))
self.link = BlockingConnection(ConnectionParameters( host,
heartbeat=self.heartbeat, credentials=PlainCredentials(*self.auth) ))
# Even with BlockingConnection adapter,
# pika doesn't raise errors, unless you set callbacks to do that
self.link.set_backpressure_multiplier(2)
self.link.add_backpressure_callback(
ft.partial(self._error_callback, 'timeout') )
self.link.add_on_close_callback(
ft.partial(self._error_callback, 'closed/error') )
self.ch = self.link.channel()
self.schema_init()
if self.tx: self.ch.tx_select() # forces flush
except (self.PikaError, socket.error) as err:
self.log.exception('Connection to AMQP broker has failed: {}'.format(err))
delay = self.reconnect_info and self.reconnect_info[0] # first delay is 0
if delay:
self.log.debug('Will retry connection in {}s'.format(delay))
sleep(delay)
self.reconnect_info = self.reconnect_info or self.reconnect_delays
if len(self.reconnect_info) > 1: self.reconnect_info = self.reconnect_info[1:]
else:
self.reconnect_info = None
break