1

我正在使用 python,并且在我做的一个类的方法中

self.connection = pika.SelectConnection(
            self.connectionParameters,
            on_open_callback=self.onConnected,
            on_open_error_callback=self.onConnectionError,
            on_close_callback=self.onConnectionClosed,
            stop_ioloop_on_close=False)

在我的源代码中,我将方法声明为(在一个类中):

def onConnectionError(self):
    """
    @summary: Called if the connection to rabbit is unavailable.  Attempt to connect to one of   
        the many backup servers.
    @return: ??
    """

但是在运行时我收到以下错误:

Traceback (most recent call last):
  File "SpaceListener.py", line 218, in <module>
    cacheTime=args.timeout)
  File "SpaceListener.py", line 88, in __init__
    logger=self.logger)
  File "/home/centos/house/tes/castExchangeScan.py", line 108, in __init__
    stop_ioloop_on_close=False)
  File "build/bdist.linux-i686/egg/pika/adapters/select_connection.py", line 51, in __init__
  File "build/bdist.linux-i686/egg/pika/adapters/base_connection.py", line 62, in __init__
  File "build/bdist.linux-i686/egg/pika/connection.py", line 590, in __init__
  File "build/bdist.linux-i686/egg/pika/connection.py", line 707, in connect
  File "build/bdist.linux-i686/egg/pika/callback.py", line 61, in wrapper
  File "build/bdist.linux-i686/egg/pika/callback.py", line 92, in wrapper
  File "build/bdist.linux-i686/egg/pika/callback.py", line 232, in process
TypeError: onConnectionError() takes exactly 1 argument (2 given)
[centos@localhost ~/house/test]$  

我一直无法找到任何显示此回调的实际方法签名的文档。我怀疑它在寻找方法的非类定义(即没有 self 作为参数)。那是对的吗?如果是这样的话?那么如何访问类变量,以便在连接错误时尝试重新连接?

我想我首先要寻找的是我的方法签名应该是什么样的?

提前致谢

4

2 回答 2

1

所以看起来我能够通过对我的代码进行以下转换来回答我自己的问题。

通过将我的代码转换为执行以下操作:

self.connection = pika.SelectConnection(self.connectionParameters,
        on_open_callback=self.onConnected, on_close_callback=self.onConnectionClosed,
        stop_ioloop_on_close=False)
self.connection.add_on_open_error_callback(self.onConnectionError)

它给了我一个新的崩溃地址,现在它将我指向 pika 中的新源代码行:

File "build/bdist.linux-i686/egg/pika/adapters/select_connection.py", line 51, in __init__
File "build/bdist.linux-i686/egg/pika/adapters/base_connection.py", line 62, in __init__
File "build/bdist.linux-i686/egg/pika/connection.py", line 590, in __init__
File "build/bdist.linux-i686/egg/pika/connection.py", line 707, in connect
File "build/bdist.linux-i686/egg/pika/callback.py", line 61, in wrapper
File "build/bdist.linux-i686/egg/pika/callback.py", line 92, in wrapper
File "build/bdist.linux-i686/egg/pika/callback.py", line 232, in process
File "build/bdist.linux-i686/egg/pika/connection.py", line 1192, in _on_connection_error
pika.exceptions.AMQPConnectionError: 1

查看 connection.py 反过来它会产生 on_connection_error 的预期方法签名

def _on_connection_error(self, connection_unused):
    """Default behavior when the connecting connection can not connect.

    :raises: exceptions.AMQPConnectionError

    """
    raise exceptions.AMQPConnectionError(self.params.connection_attempts)

因此,即使我也一样糟糕,我显然现在有了我正在寻找的方法签名。

于 2013-10-08T18:36:03.757 回答
0

只是因为我自己也是走这条路的,看来0.10.0的签名就变了。就是现在:

def _on_connection_error(self, connection_unused, error_message=None):
于 2016-01-01T00:19:42.743 回答