You can download this code by clicking the button below.
This code is now available for download.
This function establishes a connection to a random AMQP broker and randomly selects a channel from the connection. It then checks if the specified queue exists and creates it if it does not.
Technology Stack : PyAMQP, RabbitMQ
Code Type : Python Function
Code Difficulty : Intermediate
import pika
import random
def random_channel_connection(queue_name):
# Establish a connection to a random AMQP broker
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
# Create a random channel from the connection
channel = random.choice([connection.channel(), connection.channel()])
# Declare a queue if it does not exist
channel.queue_declare(queue=queue_name)
return channel