python測試rabbitmq簡易實例


生產者

import pika
#coding=utf8
credentials = pika.PlainCredentials('guest', '密碼')
connection = pika.BlockingConnection(pika.ConnectionParameters('IP',5672,'/',credentials))
channel = connection.channel()
channel.queue_declare(queue='hello')

channel.basic_publish(exchange='',
                  routing_key='hello',
                  body='rabbitmq test!')
print("開始隊列")
connection.close()

 

消費者

import pika
#coding=utf8
credentials = pika.PlainCredentials('guest', '密碼')
connection = pika.BlockingConnection(pika.ConnectionParameters('IP',5672,'/',credentials))
channel = connection.channel()


# rabbitmq消費端仍然使用此方法創建隊列。這樣做的意思是:若是沒有就創建。和發送端道理道理。目的是為了保證隊列一定會有
channel.queue_declare(queue='hello')


# 收到消息后的回調
def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)
channel.basic_consume(callback, queue='hello', no_ack=True)
print(' [*] Waiting for messages.')
channel.start_consuming()

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM