一個簡單的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()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章