本文整理汇总了Python中pika.exceptions方法的典型用法代码示例。如果您正苦于以下问题:Python pika.exceptions方法的具体用法?Python pika.exceptions怎么用?Python pika.exceptions使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pika
的用法示例。
在下文中一共展示了pika.exceptions方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: publish_message
# 需要导入模块: import pika [as 别名]
# 或者: from pika import exceptions [as 别名]
def publish_message(self, message, message_type):
if self._is_closed:
raise exceptions.ClosedAMQPClientException(
'Publish failed, AMQP client already closed')
if message_type == 'event':
exchange = self.EVENTS_EXCHANGE_NAME
else:
exchange = self.LOGS_EXCHANGE_NAME
routing_key = ''
body = json.dumps(message)
try:
self.channel.basic_publish(exchange=exchange,
routing_key=routing_key,
body=body)
except pika.exceptions.ConnectionClosed as e:
logger.warn(
'Connection closed unexpectedly for thread {0}, '
'reconnecting. ({1}: {2})'
.format(threading.current_thread(), type(e).__name__, repr(e)))
# obviously, there is no need to close the current
# channel/connection.
self._connect()
self.channel.basic_publish(exchange=exchange,
routing_key=routing_key,
body=body)
示例2: catch_error
# 需要导入模块: import pika [as 别名]
# 或者: from pika import exceptions [as 别名]
def catch_error(func):
"""Catch errors of rabbitmq then reconnect"""
import amqp
try:
import pika.exceptions
connect_exceptions = (
pika.exceptions.ConnectionClosed,
pika.exceptions.AMQPConnectionError,
)
except ImportError:
connect_exceptions = ()
connect_exceptions += (
select.error,
socket.error,
amqp.ConnectionError
)
def wrap(self, *args, **kwargs):
try:
return func(self, *args, **kwargs)
except connect_exceptions as e:
logging.error('RabbitMQ error: %r, reconnect.', e)
self.reconnect()
return func(self, *args, **kwargs)
return wrap
示例3: reconnect
# 需要导入模块: import pika [as 别名]
# 或者: from pika import exceptions [as 别名]
def reconnect(self):
"""Reconnect to rabbitmq server"""
import pika
import pika.exceptions
self.connection = pika.BlockingConnection(pika.URLParameters(self.amqp_url))
self.channel = self.connection.channel()
try:
self.channel.queue_declare(self.name)
except pika.exceptions.ChannelClosed:
self.connection = pika.BlockingConnection(pika.URLParameters(self.amqp_url))
self.channel = self.connection.channel()
#self.channel.queue_purge(self.name)
示例4: _make_connection
# 需要导入模块: import pika [as 别名]
# 或者: from pika import exceptions [as 别名]
def _make_connection(self):
exc = None
for attempt in xrange(self.CONNECTION_ATTEMPTS):
parameters = pika.ConnectionParameters(**self._connection_params_dict)
try:
return pika.BlockingConnection(parameters)
except pika.exceptions.AMQPConnectionError as exc:
time.sleep(self.CONNECTION_RETRY_DELAY)
assert exc is not None
raise exc
示例5: _handle_data
# 需要导入模块: import pika [as 别名]
# 或者: from pika import exceptions [as 别名]
def _handle_data(self, data, routing_key, custom_prop_kwargs):
if self._serialize is not None:
data = self._serialize(data)
try:
self._publish(data, routing_key, custom_prop_kwargs)
except pika.exceptions.ConnectionClosed:
if self._publishing:
self._setup_communication()
self._publish(data, routing_key, custom_prop_kwargs)
else:
# the pusher is being shut down
# => do not try to reconnect
raise
示例6: publish_data_to_queue
# 需要导入模块: import pika [as 别名]
# 或者: from pika import exceptions [as 别名]
def publish_data_to_queue(data, exchange, queue, error_msg):
""" Publish specified data to the specified queue.
Args:
data: the data to be published
exchange (str): the name of the exchange
queue (str): the name of the queue
error_msg (str): the error message to be returned in case of an error
"""
try:
with rabbitmq_connection._rabbitmq.get() as connection:
channel = connection.channel
channel.exchange_declare(exchange=exchange, exchange_type='fanout')
channel.queue_declare(queue, durable=True)
channel.basic_publish(
exchange=exchange,
routing_key='',
body=ujson.dumps(data),
properties=pika.BasicProperties(delivery_mode=2, ),
)
except pika.exceptions.ConnectionClosed as e:
current_app.logger.error("Connection to rabbitmq closed while trying to publish: %s" % str(e), exc_info=True)
raise APIServiceUnavailable(error_msg)
except Exception as e:
current_app.logger.error("Cannot publish to rabbitmq channel: %s / %s" % (type(e).__name__, str(e)), exc_info=True)
raise APIServiceUnavailable(error_msg)
示例7: close_pika_channel
# 需要导入模块: import pika [as 别名]
# 或者: from pika import exceptions [as 别名]
def close_pika_channel(
channel: "Channel",
attempts: int = 1000,
time_between_attempts_in_seconds: float = 0.001,
) -> None:
"""Attempt to close Pika channel and wait until it is closed.
Args:
channel: Pika `Channel` to close.
attempts: How many times to try to confirm that the channel has indeed been
closed.
time_between_attempts_in_seconds: Wait time between attempts to confirm closed
state.
"""
from pika.exceptions import AMQPError
try:
channel.close()
logger.debug("Successfully initiated closing of Pika channel.")
except AMQPError:
logger.exception("Failed to initiate closing of Pika channel.")
while attempts:
if channel.is_closed:
logger.debug("Successfully closed Pika channel.")
return None
time.sleep(time_between_attempts_in_seconds)
attempts -= 1
logger.exception("Failed to close Pika channel.")
示例8: close_pika_connection
# 需要导入模块: import pika [as 别名]
# 或者: from pika import exceptions [as 别名]
def close_pika_connection(connection: "Connection") -> None:
"""Attempt to close Pika connection."""
from pika.exceptions import AMQPError
try:
connection.close()
logger.debug("Successfully closed Pika connection with host.")
except AMQPError:
logger.exception("Failed to close Pika connection with host.")
示例9: __init__
# 需要导入模块: import pika [as 别名]
# 或者: from pika import exceptions [as 别名]
def __init__(self,
output_fifo_max_size=20000,
error_callback=None,
# 15 seems to be conservative enough, even paranoic a bit :-)
publishing_thread_join_timeout=15,
**kwargs):
"""
Initialize the instance and start the publishing co-thread.
Kwargs -- the same as for AMQPSimplePusher plus also:
`output_fifo_max_size` (int; default: 20000):
Maximum length of the internal output fifo.
`error_callback` (None or a callable object; default: None):
A callback to be used when an exception (being an instance
of an Exception subclass) is caught in the publishing
co-thread while trying to publish some data. The callback
will be called with the exception object as the sole
argument, in the publishing co-thread.
If there is no callback, i.e. `error_callback` is None:
the exception's traceback will be printed to sys.stderr.
If the callback throws another exception it will be caught
and its traceback will be printed to sys.stderr.
`publishing_thread_join_timeout` (int; default: 15):
Related to pusher shut down: the timeout value (in seconds)
for joining the publishing thread before checking the internal
heartbeat flag; the value should not be smaller than a
reasonable duration of one iteration of the publishing thread
loop (which includes getting a message from the inner queue,
serializing the message and sending it to the AMQP broker,
handling any exception etc.).
Raises:
A pika.exceptions.AMQPError subclass:
If AMQP connection cannot be set up.
"""
self._output_fifo = Queue.Queue(maxsize=output_fifo_max_size)
self._error_callback = error_callback
self._publishing_thread_join_timeout = publishing_thread_join_timeout
self._publishing_thread = None # to be set in _start_publishing()
self._publishing_thread_heartbeat_flag = False
super(AMQPThreadedPusher, self).__init__(**kwargs)
#
# Non-public methods