【发布时间】:2020-10-06 16:21:24
【问题描述】:
我使用 python kombu 包与 rabbitmq 服务进行交互。
我想清除所有队列。我看到有一个kombu.Queue.purge 方法,但我不想创建kombu.Queue 对象,因为我不知道哪些交换连接到哪些队列。我只想使用队列名称。
【问题讨论】:
标签: python rabbitmq message-queue
我使用 python kombu 包与 rabbitmq 服务进行交互。
我想清除所有队列。我看到有一个kombu.Queue.purge 方法,但我不想创建kombu.Queue 对象,因为我不知道哪些交换连接到哪些队列。我只想使用队列名称。
【问题讨论】:
标签: python rabbitmq message-queue
kombu Channel 类实现了一个 queue_purge 方法,该方法可以清除给定名称的队列。
以下代码列出所有队列并根据它们的名称清除它们。
from kombu import Connection
# Create a connection
mq_conn_string = 'amqp://user:password@domain:port//' # Set the correct credentials
mq_conn = Connection(mq_conn_string)
mq_conn.connect()
# Create a channel
channel = mq_conn.channel()
# Get all queues
vhost = "/"
manager = mq_conn.get_manager()
queues = manager.get_queues(vhost)
# Purge each queue
for queue in queues:
queue_name = queue["name"]
channel.queue_purge(queue_name)
【讨论】: