问题描述
我想获取传入消息正文并对其进行处理,然后将其存储在变量中,然后做我想做的任何事情,也许返回模板或其他任何东西,我不'不想使用 call_back_method 我想直接使用它并将其存储在变量中
async def consumer(exchange_name=None,exchange_type=None,queue_name=None,call_back_method=None,loop=None):
if not loop:
loop = asyncio.get_event_loop()
connection = await connect_robust(
"amqp://username:password@localhost:5672/",loop=loop
)
channel = await connection.channel()
if exchange_name and exchange_type:
exchange = await channel.declare_exchange(exchange_name,exchange_type,durable=True)
if queue_name is None:
queue = await channel.declare_queue('default')
queue = await channel.declare_queue(queue_name)
await queue.bind(exchange)
await queue.consume(callback=call_back_method)
# return call_back_method
else:
queue = await channel.declare_queue(queue_name)
await queue.consume(callback=call_back_method)
这里是我的消费者回调方法:
async def get_content(message: IncomingMessage):
async with message.process():
value = message.body.decode('utf-8')
return value.encode()
最终,这就是我想要的:
message = await consumer(queue_name="Hello",exchange_name="publisher",call_back_method=get_content)
print(message) # Message that i get from RabbitMq queue
这真的有可能吗,否则怎么解决这个问题
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)