gpt4 book ai didi

python - 这是从鼠兔中提取队列的最简单方法

转载 作者:行者123 更新时间:2023-11-28 20:08:12 26 4
gpt4 key购买 nike

好的人。我对 Python 有点生疏,所以请放轻松。我有一个连接到的 RabbitMQ 服务器,我正在构建一个简单的完整性检查界面,我正在使用 pika。

我的代码看起来很简单:

class RabbitMQConnector(object):
""" Class that instantiates the connection, and the test interface for
the RabbitMQ services.
"""

def __init__(self):
self._credentials = pika.PlainCredentials(Config.USERNAME, Config.PASSWORD)
log.info("Credentials are being set")
self._parameters = pika.ConnectionParameters(Config.HOST, credentials=self._credentials)
log.info("Connection parameters are being set")
self._connection = pika.BlockingConnection(self._parameters)
self._channel = self._connection.channel()
# We pass an empty string into the queue so we can get the random queue name
self._replyQueue = self._channel.queue_declare(queue='')
log.info("Reply queue name has been initialized %s" % self._replyQueue)
log.info("Connection initialized")
Config.time.sleep(5)
log.info("[x] System is fully initialized, and ready to accept RabbitMQ connections")

def connect(self):
pass

def disconnect(self):
pass

但是当我调用这个方法时:

log.info("Reply queue name has been initialized %s" % self._replyQueue)

我的回复是这样的:

Reply queue name has been initialized <METHOD(['channel_number=1', 'frame_type=1', "method=<Queue.DeclareOk(['consumer_count=0', 'message_count=0', 'queue=amq.gen-8GVTfmr8noMB5slpXnFRHQ'])>"])>

我唯一想向用户显示的实际上是这个队列

queue=amq.gen-8GVTfmr8noMB5slpXnFRHQ

从上述方法中提取该变量的最简单方法是什么?谢谢。

p/s: log 只是 logger 的别名,而 Config.USERNAMEConfig.PASSWORD 只是 'user' 和'本地主机'

最佳答案

来自3rd rabbitmq tutorial , 接收器代码:

result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue

引用上述链接(在标题 临时队列 下):

...we could create a queue with a random name, or, even better - let the server choose a random queue name for us. We can do this by not supplying the queue parameter to queue_declare:

result = channel.queue_declare()
At this point result.method.queue contains a random queue name.

关于python - 这是从鼠兔中提取队列的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40385640/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com