Как подключиться к Rabbitmq - PullRequest
       4

Как подключиться к Rabbitmq

0 голосов
/ 16 января 2020

Я использую Пика 1.1.0, Python 3.7.4. В теме есть потребитель. Я хочу убить поток и закрыть соединение rabbitmq, но это не удается. Где я делаю не так? Как я могу это сделать?

Ошибка: pika.exceptions.StreamLostError: Потоковое соединение потеряно: IndexError ('pop from empty deque')

def BrokerConnection(username, password):
    try:
        credentials = pika.PlainCredentials(username,password)
        parameters = pika.ConnectionParameters("127.0.0.1","5672","vhost",credentials, heartbeat=0)
        connection = pika.BlockingConnection(parameters)
        channel = connection.channel()
        return connection, channel
    except:
        return None, None

def BrokerListen():
    def BrokerConnect():        
        while True:
            try:
                queue = "test-que"
                connection, channel = BrokerConnection("admin", "123345")

                if channel is not None:
                    brokerConnections.update({"key1":channel})
                    return connection, channel
            except:
                time.sleep(1)
                print("error")

    connection, channel, queue = BrokerConnect()    
    print(f'[*] Waiting for {queue} messages. To exit press CTRL+C')

    def callback(ch, method, properties, body):

        data = json.loads(body)
        print(data)

        ch.basic_ack(delivery_tag=method.delivery_tag)

    channel.basic_consume(queue=queue, on_message_callback=callback)
    channel.start_consuming()

def ConnectionClose():
    channel = brokerConnections["key1"]
    if channel.is_open:
        connection = channel.connection
        channel.stop_consuming()
        connection.close()
        del brokerConnections["key1"]
...