Python Pika блокирует соединение на выделенном классе - обратный вызов не вызывается - PullRequest
0 голосов
/ 21 апреля 2020

Когда я запускаю простое приложение (производитель / потребитель) - все работает. он подключается к rabbitmq, потребляя msgs et c.

, когда я запускаю приведенный ниже код внутри класса (чтобы не блокировать приложение flask) -> обратный вызов не вызывается.

Я добавил несколько отпечатков, и кажется, что все в порядке во время инициализации соединения.

Сообщения израсходованы (очередь чистая) -> но обратный вызов даже не вызывается. Есть идеи ? TIA

вот код:

currentprices = ({"1":5.5, "2":3.5})

def initialparams(): #being called from outside
    print('S-init')
    RMQ = RMQPriceListener()
    RMQ.create_currentprices_channel()
    print('F-init')


class RMQPriceListener():
    def create_currentprices_channel(self):
        try:
            credential_params = pika.PlainCredentials('un', 'pw')
            connection_params = pika.ConnectionParameters(
                                host='127.0.0.1',port=5555,
                                credentials=credential_params) # i use port 5555

            connection = pika.BlockingConnection(connection_params)
            channel = connection.channel()
            channel.queue_declare(queue='formatedrates')
            print('c0') # being printed
            channel.basic_consume(queue='formatedrates',
                                  on_message_callback=self.callback,
                                  auto_ack=True) # without this (above) self - the app 
                                                 #  is stuck here (which is logical) 
            print('c1') # being printed
            channel.start_consuming() # working good and cleaning the queue

            return  ('lost connection to RMQ')
        except:
            return ('No connection to RMQ')

** проблемный c забавный c, который хорошо работает в обычном приложении, но здесь внутри класса не **

 def callback(ch, method, properties,msg):
       print(f'msg={msg}')     # never printed
       global currentprices    # has an initial value and being call from the below  
                                 getcurrentrates method and never change its value
       currentprices = msg     # never being updated 
       print(currentprices)    # never printed


def getcurrentrates():
    print('get' + str(currentprices)) # printed
    print(type(currentprices)) # printed - ' dict '
    return currentprices       # returned with  initial value
...