1. 程式人生 > >python rabbit MQ

python rabbit MQ

art declare hang ... ons queue callback chang nec

produce:

import pika
connection = pika.BlockingConnection(pika.ConnectionParameters("localhost") )
channel=connection.channel()
channel.queue_declare(queue="hello")
channel.basic_publish(exchange=‘‘,routing_key="hello",body="Hello World")
print("sent ‘hello world")
connection.close()

consumer:
import pika
connection=pika.BlockingConnection (pika.ConnectionParameters("localhost"))
channel=connection.channel()
channel.queue_declare(queue="hello")
def callback(ch,method,properties,body):
# print(ch)
# print(method)
# print(properties)
print("Received %r"%body)
channel.basic_consume(callback,queue=‘hello‘,no_ack=True)
print("wait ....")
channel.start_consuming()

python rabbit MQ