Consume multiple queues in python / pika

后端 未结 2 911
青春惊慌失措
青春惊慌失措 2021-02-05 01:08

I am trying to create a consumer that would subscribe to multiple queues, and then process messages as they arrive.

The problem is that when there is some data already

相关标签:
2条回答
  • 2021-02-05 01:28

    The issue is most likely that the first call has issued a Basic.Consume and has already received messages from a pre-populated queue before the second call is issued. You might want to try setting the QoS prefetch count to 1, which will limit RabbitMQ from sending you more than one message at a time.

    0 讨论(0)
  • 2021-02-05 01:39

    One possible solution is to use non blocking connection and consume messages.

    import pika
    
    
    def callback(channel, method, properties, body):
        print(body)
        channel.basic_ack(delivery_tag=method.delivery_tag)
    
    
    def on_open(connection):
        connection.channel(on_channel_open)
    
    
    def on_channel_open(channel):
        channel.basic_consume(callback, queue='queue1')
        channel.basic_consume(callback, queue='queue2')
    
    
    parameters = pika.URLParameters('amqp://guest:guest@localhost:5672/%2F')
    connection = pika.SelectConnection(parameters=parameters,
                                       on_open_callback=on_open)
    
    try:
        connection.ioloop.start()
    except KeyboardInterrupt:
        connection.close()
    

    This will connect to multiple queues and will consume messages accordingly.

    0 讨论(0)
提交回复
热议问题