gpt4 book ai didi

python - Pika exchange_declare 回调中的交易所名称

转载 作者:行者123 更新时间:2023-11-28 22:53:21 24 4
gpt4 key购买 nike

我尝试在我的 exchange_declare 回调中使用交易所名称。我将 SelectConnection 适配器用于 Pika 中的异步请求。

我的想法是首先在我的程序中构建交换器、队列和绑定(bind)的列表。然后,我将多个交换和队列声明分派(dispatch)给 Pika,并在每个绑定(bind)所需的队列和交换收到 DeclareOK 消息后立即创建绑定(bind)。

这意味着我需要知道我的回调中交换器和队列的名称,以便我可以将它们与要创建的绑定(bind)相关联。

像这样:

print "Create A"
channel.exchange_declare(callback=on_exchange, exchange="exchangeA")
print "Create B"
channel.exchange_declare(callback=on_exchange, exchange="exchangeB")

def on_exchange(response):
# How do I know if this was exchangeA or exchangeB ?
print "Exchange declared"
print response

这给出了以下输出,清楚地表明交换是先声明的,回调是后触发的(如预期的那样):

Create A
Create B
Exchange declared
<METHOD(['method=<Exchange.DeclareOk>', 'channel_number=1', 'frame_type=1'])>
Exchange declared
<METHOD(['method=<Exchange.DeclareOk>', 'channel_number=1', 'frame_type=1'])>

我发现 response 的类型是 pika.frame.Method但我该怎么做呢?交易所是否可以保证按顺序申报?

最佳答案

闭包是一种保持代码异步的解决方案:

def declare_exchanges(self):
exchange = "exchangeA"
callback = self.on_exchange(exchange)
channel.exchange_declare(callback=callback, exchange=exchange)

exchange = "exchangeB"
callback = self.on_exchange(exchange)
channel.exchange_declare(callback=callback, exchange=exchange)

def on_exchange(self, exchange):
def callback(response):
print(exchange)

return callback

PS:我在上游开了个issue,https://github.com/pika/pika/issues/898

关于python - Pika exchange_declare 回调中的交易所名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19500319/

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