rabbit consume mssage

#949
Raw
Author
Anonymous
Created
Nov. 20, 2023, 12:59 p.m.
Expires
Never
Size
698 bytes
Hits
21
Syntax
Python
Private
No
import pika

def callback(ch, method, properties, body):
    print(f" [x] Received {body}")

def consume_message():
    # Establish a connection to RabbitMQ server
    connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
    channel = connection.channel()

    # Declare a queue named 'hello'
    channel.queue_declare(queue='hello')

    # Set up a consumer and specify the callback function
    channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=True)

    print(' [*] Waiting for messages. To exit, press Ctrl+C')
    # Start consuming messages
    channel.start_consuming()

if __name__ == '__main__':
    consume_message()