Untitled

mail@pastecode.io avatar
unknown
python
a year ago
750 B
6
Indexable
from celery import shared_task
import pika

@shared_task
def process_rabbitmq_messages():
    # Establish a connection to RabbitMQ
    connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
    channel = connection.channel()

    # Declare a queue
    channel.queue_declare(queue='my_queue')

    # Define the callback function for processing messages
    def callback(ch, method, properties, body):
        print("Received:", body)
        # Perform your processing on the message here

    # Set the callback function to consume messages
    channel.basic_consume(queue='my_queue', on_message_callback=callback, auto_ack=True)

    # Start consuming messages
    print("Waiting for messages...")
    channel.start_consuming()