2015-07-13 60 views
0

我想創建一個簡單的Python Pika SelectConnection,看來我無法使用on_open_callback打開連接,並且我也沒有從te on_open_error_callback獲取任何內容。有人可以提出可能導致問題的原因嗎?Python Pika回調不能使用類內的SelectConnection

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:[email protected]: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() 

回答

1

發現問題,我添加了下面的行,然後連接打開,回調工作。

self._connection.ioloop.start()