一个简单的rabbitmq

生产者

import pika

credentials = pika.PlainCredentials("Username","Password")
connections = pika.BlockingConnection(pika.ConnectionParameter("ip",credentials=credentials))
channel = connections.channel()

channel.queue_declare("test1")

# 生产消息到队列
channel.basic_publish(exchange="",routing_key="test1",body="I am a body")

channel.close()

消费者

import pika


credential = pika.PlainCredential("username", "password")
connection = pika.BlockingConnecton(pika.ConnectionParameters("ip",credentials=credential))
channel = connection.channel()

channel.queue_declare("test")

def callback_function(ch,method,properties,body)
    print("consume done",ch,method,body)


channel.basic_consume(on_message_callback=callback_function,queue="test",auto_ack=True)
chanmel.start_consuming()

 

你可能感兴趣的:(rabbitmq)