Python AMQPConnectionError将Pika和RabbitMQ与Asyncore一起使用-为什么?

Python AMQPConnectionError将Pika和RabbitMQ与Asyncore一起使用-为什么?,python,rabbitmq,pika,Python,Rabbitmq,Pika,为什么使用Asyncore而不是BlockingConnection时会出现AMQPConnectionError? 如果只是“Asyncore在Windows中不起作用”,那就随它去吧,尽管我还没有找到任何禁止它使用的东西。(这个问题与平台无关。)为了便于迁移,我想使用Python 2.7和Python 3.4上都提供的异步库,异步内核应该在这里工作 我将RabbitMQ 3.2.4与Python 2.7.6和pika 0.9.13一起使用。用户和管理员的运行级别没有区别。记录器在代码中的存在

为什么使用Asyncore而不是BlockingConnection时会出现AMQPConnectionError?

如果只是“Asyncore在Windows中不起作用”,那就随它去吧,尽管我还没有找到任何禁止它使用的东西。(这个问题与平台无关。)为了便于迁移,我想使用Python 2.7和Python 3.4上都提供的异步库,异步内核应该在这里工作

我将RabbitMQ 3.2.4与Python 2.7.6和pika 0.9.13一起使用。用户和管理员的运行级别没有区别。记录器在代码中的存在或不存在与错误无关,但上面更新的警告消息除外。Linux(Ubuntu14.04)和Windows7中也出现了同样的错误,所以这不是平台问题

因为pika使用BlockingConnection的性能相当差,所以我想尝试使用Asyncore适配器。对于测试台设置来说似乎非常简单(我尝试给它提供凭据,但这并不重要,如果不提供凭据,回调将被终止……无论哪种方式都会失败。):

根据教程使用BlockingConnection-它可以工作,但吞吐量较低:

connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
使用AsyncoreConnection-我尝试过的所有变体都会立即失败:

connection = pika.AsyncoreConnection(pika.ConnectionParameters(host='localhost'))
错误:

警告:pika。连接:无法连接,剩余0次尝试
回溯(最近一次呼叫最后一次):
文件“C:\workspace\send.py”,第8行,在
connection=pika.AsyncoreConnection(pika.ConnectionParameters(host='localhost'))
文件“C:\Python27\lib\site packages\pika\adapters\asyncore\u connection.py”,第135行,在\uuu init中__
停止(打开或关闭)
文件“C:\Python27\lib\site packages\pika\adapters\base\u connection.py”,第62行,在\uu init中__
打开(关闭(回调)
文件“C:\Python27\lib\site packages\pika\connection.py”,第590行,在\uuu init中__
self.connect()
文件“C:\Python27\lib\site packages\pika\connection.py”,第707行,在connect中
self.callbacks.process(0,self.ON\u连接错误,self,self)
文件“C:\Python27\lib\site packages\pika\callback.py”,第61行,在包装器中
返回函数(*元组(args),**kwargs)
文件“C:\Python27\lib\site packages\pika\callback.py”,第92行,在包装器中
返回函数(*args,**kwargs)
文件“C:\Python27\lib\site packages\pika\callback.py”,第232行,正在处理中
回调(*参数,**关键字)
文件“C:\Python27\lib\site packages\pika\connection.py”,第1192行,在连接错误中
引发异常.AMQPConnectionError(self.params.connection\u尝试)
pika.exceptions.amqConnectionError:1
阅读这篇文章:

修正加入:

import logging
logging.basicConfig()
编辑

该问题已被报道

请阅读以下帖子:

修正加入:

import logging
logging.basicConfig()
编辑


这个问题已经被报道了

在我看来,它实际上就像皮卡的一个bug。下面是最终引发异常的connection.connect()代码:

def connect(self):
    """Invoke if trying to reconnect to a RabbitMQ server. Constructing the
    Connection object should connect on its own.

    """
    self._set_connection_state(self.CONNECTION_INIT)
    if self._adapter_connect():
        return self._on_connected()
    self.remaining_connection_attempts -= 1
    LOGGER.warning('Could not connect, %i attempts left',
                   self.remaining_connection_attempts)
    if self.remaining_connection_attempts:
        LOGGER.info('Retrying in %i seconds', self.params.retry_delay)
        self.add_timeout(self.params.retry_delay, self.connect)
    else:
        self.callbacks.process(0, self.ON_CONNECTION_ERROR, self, self)
        self.remaining_connection_attempts = self.params.connection_attempts
        self._set_connection_state(self.CONNECTION_CLOSED)
因此,
self.\u adapter\u connect()
显然没有返回True,这表明连接失败。这是
异步连接。\u适配器\u连接
代码:

def _adapter_connect(self):
    """Connect to our RabbitMQ broker using AsyncoreDispatcher, then setting
    Pika's suggested buffer size for socket reading and writing. We pass
    the handle to self so that the AsyncoreDispatcher object can call back
    into our various state methods.

    """
    if super(AsyncoreConnection, self)._adapter_connect():
        self.socket = PikaDispatcher(self.socket, None, self._handle_events)
        self.ioloop = self.socket
        self._on_connected()
它不会返回任何东西!因此,
connect
中的if语句永远不会为真。如果我更改方法以反映所有其他适配器使用的模式:

def _adapter_connect(self):
    """Connect to our RabbitMQ broker using AsyncoreDispatcher, then setting
    Pika's suggested buffer size for socket reading and writing. We pass
    the handle to self so that the AsyncoreDispatcher object can call back
    into our various state methods.

    """
    if super(AsyncoreConnection, self)._adapter_connect():
        self.socket = PikaDispatcher(self.socket, None, self._handle_events)
        self.ioloop = self.socket
        return True
    return False
它很好用。我一定会把那个bug归档的

编辑:

该错误似乎已在最新版本中修复(从):


在我看来,它实际上就像皮卡舞中的一只虫子。下面是最终引发异常的connection.connect()代码:

def connect(self):
    """Invoke if trying to reconnect to a RabbitMQ server. Constructing the
    Connection object should connect on its own.

    """
    self._set_connection_state(self.CONNECTION_INIT)
    if self._adapter_connect():
        return self._on_connected()
    self.remaining_connection_attempts -= 1
    LOGGER.warning('Could not connect, %i attempts left',
                   self.remaining_connection_attempts)
    if self.remaining_connection_attempts:
        LOGGER.info('Retrying in %i seconds', self.params.retry_delay)
        self.add_timeout(self.params.retry_delay, self.connect)
    else:
        self.callbacks.process(0, self.ON_CONNECTION_ERROR, self, self)
        self.remaining_connection_attempts = self.params.connection_attempts
        self._set_connection_state(self.CONNECTION_CLOSED)
因此,
self.\u adapter\u connect()
显然没有返回True,这表明连接失败。这是
异步连接。\u适配器\u连接
代码:

def _adapter_connect(self):
    """Connect to our RabbitMQ broker using AsyncoreDispatcher, then setting
    Pika's suggested buffer size for socket reading and writing. We pass
    the handle to self so that the AsyncoreDispatcher object can call back
    into our various state methods.

    """
    if super(AsyncoreConnection, self)._adapter_connect():
        self.socket = PikaDispatcher(self.socket, None, self._handle_events)
        self.ioloop = self.socket
        self._on_connected()
它不会返回任何东西!因此,
connect
中的if语句永远不会为真。如果我更改方法以反映所有其他适配器使用的模式:

def _adapter_connect(self):
    """Connect to our RabbitMQ broker using AsyncoreDispatcher, then setting
    Pika's suggested buffer size for socket reading and writing. We pass
    the handle to self so that the AsyncoreDispatcher object can call back
    into our various state methods.

    """
    if super(AsyncoreConnection, self)._adapter_connect():
        self.socket = PikaDispatcher(self.socket, None, self._handle_events)
        self.ioloop = self.socket
        return True
    return False
它很好用。我一定会把那个bug归档的

编辑:

该错误似乎已在最新版本中修复(从):


尝试下面提到的步骤。我在centos机器上也遇到了同样的问题

  • sudo yum安装rabbitmq服务器
  • sudo服务rabbitmq服务器重启

  • 尝试下面提到的步骤。我在centos机器上也遇到了同样的问题

  • sudo yum安装rabbitmq服务器
  • sudo服务rabbitmq服务器重启

  • 您必须通过运行下面的命令来启动rabbitmq服务器,然后它才能工作


    sudo systemctl start rabbitmq server

    您必须通过运行下面的命令来启动rabbitmq服务器,然后它才能工作


    sudo systemctl start rabbitmq server

    好吧,我得到的不是“没有记录器”(这似乎更像是一个通知而不是一个错误),而是“警告:pika.connection:无法连接,剩余0次尝试”。。。同样的“pika.exceptions.AMQPConnectionError:1”仍然存在。我将对问题进行编辑,以澄清问题的关键不是记录器,而是连接错误。好吧,我非常感谢有关日志记录的提示。。。我只是被Asyncore问题弄糊涂了。可能会有错误。对不起,你说得对!我的macWell也有同样的问题,而不是“没有记录者”(这似乎更像是一个通知而不是一个错误),我得到了“警告:pika。连接:无法连接,剩余0次尝试”。。。同样的“pika.exceptions.AMQPConnectionError:1”仍然存在。我将对问题进行编辑,以澄清问题的关键不是记录器,而是连接错误。好吧,我非常感谢有关日志记录的提示。。。我只是被Asyncore问题弄糊涂了。可能会有错误。对不起,你说得对!我的电脑上也有同样的问题谢谢!我不久前提交了一个bug()。我将添加一个引用此线程的注释。。。考虑到我的标准(我认为Tornado可能是一个更好的2.7/3.4解决方案),也许我对Asyncore的选择不是最优的,但它仍然应该有效