1
我正在使用一个用于发送和接收消息到服务器的Python的RabbitMQ服务器 这是我用来发送消息到代码中的代码。如何存储rabbitMQ消息
import numpy as np
import pandas as pd
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='Q1')
message = 'Hello World'
channel.basic_publish(exchange='',
routing_key='Q1',
body=message)
# Printing the Sending Confirmation of ID
print(" [x] Sent %r" % message)
connection.close()
输出:
[x] Sent 'Hello World'
这是我使用用于从队列接收消息
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='Q1')
def callback(ch, method, properties, body):
print(" [x] Received %r" % body)
channel.basic_consume(callback, queue='Q1', no_ack=True)
channel.start_consuming()
输出的代码:
[x] Received 'Hello World'
问题是我想将此消息(即“Hello World”)保存到变量中,然后在我的程序中使用它
但我无法保存消息。
如何将其保存到变量。 会有什么在队列中多条消息的解决方案