python rabbit MQ
阿新 • • 發佈:2017-11-05
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