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」)保存到變量中,然後在我的程序中使用它
但我無法保存消息。
如何將其保存到變量。 會有什麼在隊列中多條消息的解決方案