You can download this code by clicking the button below.
This code is now available for download.
This function is used to send a message to a specified queue in RabbitMQ. It first establishes a connection to the RabbitMQ server, then declares a queue if it does not exist, sends the message to the queue, and finally closes the connection.
Technology Stack : RMQ third-party library
Code Type : Function
Code Difficulty : Intermediate
def send_message_to_queue(queue_name, message):
import pika
# Create a connection to the RabbitMQ server
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
# Declare a queue if it does not exist
channel.queue_declare(queue=queue_name)
# Send a message to the queue
channel.basic_publish(exchange='', routing_key=queue_name, body=message)
# Close the connection
connection.close()