pika模块的使用中,如何动态的删除一个 durable=True 的持久化队列?
比如某个队列 'rpc_queue' 是以前由其他python脚本创建的持久化队列,在rabbitmq中一直存在着。
在某次写脚本时,也申明了一个同名的队列,但不是持久化的,就会报错。
如果直接删除再创建,当然可以,像这样:
channel.queue_delete(queue='rpc_queue')
channel.queue_declare(queue='rpc_queue')
但实际上,创建队列时并不能确定某队列是否已存在,如果用try的方式,像这样
try:
channel.queue_declare(queue='rpc_queue')
except:
channel.queue_delete(queue='rpc_queue')
channel.queue_declare(queue='rpc_queue')
也会报错。报错信息:
Traceback (most recent call last):
File "D:\rpc_server.py", line 11, in <module>
channel.queue_declare(queue='rpc_queue')
File "D:\liuzhibo\Python35\lib\site-packages\pika\adapters\blocking_connection.py", line 2329, in queue_declare
self._flush_output(declare_ok_result.is_ready)
File "D:\liuzhibo\Python35\lib\site-packages\pika\adapters\blocking_connection.py", line 1181, in _flush_output
raise exceptions.ChannelClosed(method.reply_code, method.reply_text)
pika.exceptions.ChannelClosed: (406, "PRECONDITION_FAILED - inequivalent arg 'durable' for queue 'rpc_queue' in vhost '/': received 'false' but current is 'true'")
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\rpc_server.py", line 13, in <module>
channel.queue_delete(queue='rpc_queue')
File "D:\liuzhibo\Python35\lib\site-packages\pika\adapters\blocking_connection.py", line 2351, in queue_delete
nowait=False)
File "D:\liuzhibo\Python35\lib\site-packages\pika\channel.py", line 717, in queue_delete
self._validate_channel_and_callback(callback)
File "D:\liuzhibo\Python35\lib\site-packages\pika\channel.py", line 1179, in _validate_channel_and_callback
raise exceptions.ChannelClosed()
pika.exceptions.ChannelClosed
channel对象下貌似也没有判断一个队列是否存在的方法,那要怎么样动态的判断一个队列是否存在并删除之?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误很明显嘛,重新拿
channel
就好了:可以使用rabbitmq自带的rabbitmqctl.bat:
import subprocess
p = subprocess.Popen(r'"C:Program FilesRabbitMQ Serverrabbitmq_server-3.6.1sbinrabbitmqctl.bat" list_queues', shell=True, stdout=subprocess.PIPE)
print p.communicate()[0]