Python pika 模块,exceptions() 实例源码

我们从Python开源项目中,提取了以下18个代码示例,用于说明如何使用pika.exceptions()

项目:frontoxy    作者:fabienvauchelles    | 项目源码 | 文件源码
def publish(self, item, priority=0, retry=2):
        body = json.dumps(item)

        try:
            self._channel.basic_publish(exchange=u'',
                                        routing_key=self._queue_name,
                                        body=body,
                                        properties=pika.BasicProperties(
                                            delivery_mode=2,
                                            priority=priority
                                        ))

        except exceptions.ConnectionClosed as err:
            if retry <= 0:
                raise err

            self.open()
            self.publish(item, retry - 1)
项目:plumpy    作者:aiidateam    | 项目源码 | 文件源码
def setUp(self):
        super(TestStatusRequesterAndProvider, self).setUp()

        self.response = None

        # Set up communications
        try:
            self._connection = pika.BlockingConnection()
        except pika.exceptions.ConnectionClosed:
            self.fail("Couldn't open connection.  Make sure rmq server is running")

        exchange = "{}.{}.status_request".format(self.__class__, uuid.uuid4())
        self.requester = StatusRequester(self._connection, exchange=exchange)
        self.manager = ProcessManager()
        self.provider = StatusProvider(
            self._connection, process_manager=self.manager, exchange=exchange)
项目:plumpy    作者:aiidateam    | 项目源码 | 文件源码
def setUp(self):
        super(TestProcessController, self).setUp()
        try:
            self._connection = pika.BlockingConnection()
        except pika.exceptions.ConnectionClosed:
            self.fail("Couldn't open connection.  Make sure rmq server is running")

        self.exchange = '{}.{}.task_control'.format(
            self.__class__, uuid.uuid4())

        self.channel = self._connection.channel()
        self.channel.exchange_declare(exchange=self.exchange, type='fanout')

        self.manager = ProcessManager()
        self.controller = ProcessController(
            self._connection, exchange=self.exchange,
            process_manager=self.manager)
项目:frontoxy    作者:fabienvauchelles    | 项目源码 | 文件源码
def _publish(self, exchange_name, queue_name, body, priority, retry):
        try:
            self._channel.basic_publish(exchange=exchange_name,
                                        routing_key=queue_name,
                                        body=body,
                                        properties=pika.BasicProperties(
                                            delivery_mode=2,
                                            priority=priority
                                        ))

        except exceptions.ConnectionClosed as err:
            if retry <= 0:
                raise err

            self.open()
            self._publish(exchange_name, queue_name, body, priority, retry - 1)
项目:ISB-CGC-pipelines    作者:isb-cgc    | 项目源码 | 文件源码
def __del__(self):
        try:
            self._connection.close()

        except pika.exceptions.AMQPError as e:
            pass
项目:ISB-CGC-pipelines    作者:isb-cgc    | 项目源码 | 文件源码
def publishToExchange(self, exchangeName, routingKey, msg):
        try:
            self._channel.basic_publish(exchange=exchangeName, routing_key=routingKey, body=msg)

        except pika.exceptions.AMQPError as e:
            raise PipelineQueueError("Couldn't push message to exchange: {reason}".format(reason=e))
项目:ISB-CGC-pipelines    作者:isb-cgc    | 项目源码 | 文件源码
def __del__(self):
        try:
            self._connection.close()

        except pika.exceptions.AMQPError as e:
            pass
项目:ISB-CGC-pipelines    作者:isb-cgc    | 项目源码 | 文件源码
def __del__(self):
        try:
            self._connection.close()

        except pika.exceptions.AMQPError as e:
            pass
项目:ISB-CGC-pipelines    作者:isb-cgc    | 项目源码 | 文件源码
def publish(self, msg):
        try:
            self._channel.basic_publish(exchange=self._exchange, routing_key=self._qname, body=msg)

        except pika.exceptions.AMQPError as e:
            raise PipelineQueueError("Couldn't push message to queue: {reason}".format(reason=e))
项目:ISB-CGC-pipelines    作者:isb-cgc    | 项目源码 | 文件源码
def acknowledge(self, method):
        if method:
            try:
                self._channel.basic_ack(method.delivery_tag)

            except pika.exceptions.AMQPError as e:
                raise PipelineQueueError("Couldn't acknowledge message: {reason}".format(reason=e))
项目:Url    作者:beiruan    | 项目源码 | 文件源码
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
项目:Url    作者:beiruan    | 项目源码 | 文件源码
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)
项目:Url    作者:beiruan    | 项目源码 | 文件源码
def reconnect(self):
        """Reconnect to rabbitmq server"""
        parsed = urlparse.urlparse(self.amqp_url)
        port = parsed.port or 5672
        self.connection = amqp.Connection(host="%s:%s" % (parsed.hostname, port),
                                          userid=parsed.username or 'guest',
                                          password=parsed.password or 'guest',
                                          virtual_host=unquote(
                                              parsed.path.lstrip('/') or '%2F'))
        self.channel = self.connection.channel()
        try:
            self.channel.queue_declare(self.name)
        except amqp.exceptions.PreconditionFailed:
            pass
        #self.channel.queue_purge(self.name)
项目:plumpy    作者:aiidateam    | 项目源码 | 文件源码
def setUp(self):
        super(TestStatusProvider, self).setUp()
        self._response = None
        self._corr_id = None

        try:
            self._connection = pika.BlockingConnection()
        except pika.exceptions.ConnectionClosed:
            self.fail("Couldn't open connection.  Make sure rmq server is running")

        self.channel = self._connection.channel()

        # Set up the request exchange
        self.request_exchange = '{}.{}.task_control'.format(
            self.__class__, uuid.uuid4())
        self.channel.exchange_declare(exchange=self.request_exchange, type='fanout')

        # Set up the response queue
        result = self.channel.queue_declare(exclusive=True)
        self.response_queue = result.method.queue
        self.channel.basic_consume(
            self._on_response, no_ack=True, queue=self.response_queue)

        self.manager = ProcessManager()
        self.provider = StatusProvider(
            self._connection, exchange=self.request_exchange,
            process_manager=self.manager)
项目:plumpy    作者:aiidateam    | 项目源码 | 文件源码
def setUp(self):
        super(TestTaskControllerAndRunner, self).setUp()

        try:
            connection = pika.BlockingConnection()
        except pika.exceptions.ConnectionClosed:
            self.fail("Couldn't open connection.  Make sure rmq server is running")

        queue = "{}.{}.tasks".format(self.__class__, uuid.uuid4())
        self.sender = TaskController(connection, queue=queue)
        self.runner = TaskRunner(connection, queue=queue)
项目:frontoxy    作者:fabienvauchelles    | 项目源码 | 文件源码
def get(self, retry=2):
        try:
            method_frame, properties, body = self._channel.basic_get(self._queue_name, no_ack=True)
            if not body:
                return

            return json.loads(body)

        except exceptions.ConnectionClosed as err:
            if retry <= 0:
                raise err

            self.open()
            return self.get(retry - 1)
项目:frontoxy    作者:fabienvauchelles    | 项目源码 | 文件源码
def get(self, retry = 2):
        try:
            method_frame, properties, body = self._channel.basic_get(self._queue_name, no_ack=True)
            if not body:
                return

            return json.loads(body)

        except exceptions.ConnectionClosed as err:
            if retry <= 0:
                raise err

            self.open()
            return self.get(retry - 1)
项目:reproserver    作者:ViDA-NYU    | 项目源码 | 文件源码
def _retry(self, f):
        while True:
            try:
                return f()
            except pika.exceptions.ConnectionClosed:
                logging.exception("AMQP connection is down...")
                time.sleep(1)
            self._connect()