0. Offical Doc
https://www.rabbitmq.com/docs
1. Install RabbitMQ with docker
1
| docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:4-management
|
2. Using the Pika Python client
send.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('10.1.125.112')) channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='', routing_key='hello', body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close()
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('10.1.125.112')) channel = connection.channel()
def callback(ch, method, properties, body): print(f" [x] Received {body}")
channel.basic_consume(queue='hello', auto_ack=True, on_message_callback=callback) channel.queue_declare(queue='hello')
print(' [*] Waiting for messages. To exit press CTRL+C') channel.start_consuming()
|