Can't purge rabbitmq queue with pika

724 Views Asked by At

My producer code:

conn_params = pika.ConnectionParameters('localhost', 5672,  '/')
connection = pika.BlockingConnection(conn_params)
channel = connection.channel()
channel.exchange_declare(exchange='direct_exchange',
                         exchange_type='direct')
channel.queue_declare(queue='my_queue')

@socketIo.on("clean")
def clean(msg):
    print("cleaning")
    channel.queue_purge('my_queue')

My consumer code:

conn_params = pika.ConnectionParameters('localhost', 5672, '/')
connection = pika.BlockingConnection(conn_params)
channel = connection.channel()
channel.exchange_declare(exchange='direct_exchange', exchange_type='direct')
result = channel.queue_declare(queue='my_queue')
channel.queue_bind(exchange='direct_exchange', queue='my_queue', routing_key='my_queue')


def callback(ch, method, properties, body):
    ...
    channel.basic_ack(delivery_tag=method.delivery_tag)

channel.basic_consume('my_queue', callback, auto_ack=False )
channel.basic_qos(prefetch_count=1)

I tried everything I could find but still the messages stay in the queue. And also I tried to check number of messages in the queue but it always shows 0 both in producer and performer:

res = channel.queue_declare(
        queue="my_queue",
        passive=True
    )
print('Messages in queue {}'.format(res.method.message_count)
1

There are 1 best solutions below

0
On

Sorry for the necroposting, but this might help someone. This is what I do to connect to a queue and purge it, before start using it (and it works):

from pika import URLParameters, BlockingConnection

rabbit_host = 'amqp://localhost:5672'
rabbit_queue = 'my_fantastic_queue'

rabbit_connection = BlockingConnection(URLParameters(rabbit_host))
rabbit_channel = rabbit_connection.channel()
rabbit_channel.queue_purge(rabbit_queue)

Tune variables rabbit_host and rabbit_queue according to your needs.