RabbitMQ之tutorial-1

RabbitMQ之tutorial-1

Tutorial One

https://www.rabbitmq.com/tutorials/tutorial-one-python.html

python send.py
python receive.py

send.py

import pika

connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost'))
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()

receive.py

import pika

connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost'))
channel = connection.channel()

channel.queue_declare(queue='hello')

def callback(ch, method, props, body):
print(" [x] Received %r" % body)

channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')

channel.start_consuming()

testing

python send.py
python receive.py