Python Pika回调在类内使用SelectConnection不起作用

2024-10-04 09:25:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个简单的Python Pika SelectConnection,但似乎我无法使用on_open_回调打开连接,而且也没有从te on_open_error_回调中获得任何信息。有人能提出问题的原因吗?在

import pika

class RabbitmqTransport(object):

    def __init__(self):

        self._connection = None
        self._channel = None
        self._connect()

    def on_connection_open(self):
        print "connection created"

    def on_connection_open_error(self):
        print "connection open error"

    def _connect(self):
        # Setup RabbitMQ connection
        credentials = pika.PlainCredentials('guest','guest')
        parameters = pika.URLParameters('amqp://guest:guest@localhost:5672/%2F')


        print "Creating Connection"
        self._connection = pika.SelectConnection(parameters=parameters,on_open_callback=self.on_connection_open,on_open_error_callback=self.on_connection_open_error)
        print self._connection.connection_state
        print dir(self._connection)
        print self._connection.is_open

r = RabbitmqTransport()

Tags: selfnoneondefconnectcallbackerroropen