python rabbitmq 初识

来源:互联网 发布:码字赚钱的软件 编辑:程序博客网 时间:2024/04/27 21:36
基于模块pika实现
send.py
#_*_coding:utf-8_*_import pikaimport  timeconnection = pika.BlockingConnection(pika.ConnectionParameters("192.168.140.78",5672))channel =connection.channel()channel.queue_declare(queue="hello") #创建一个queuechannel.basic_publish(exchange="",routing_key="hello",body="helloworld!")print("send 'helloworld'")connection.close()#消息发送后关闭,确保缓冲器资料全部被送出(flush)
recieve.py
import pikaconnection =pika.BlockingConnection(pika.ConnectionParameters("192.168.140.78",78))channel = connection.channel()channel.queue_declare(queue="hello")def callback(ch,method,properties,body):    print("receive")channel.basic_consume(callback,queue="hello",no_ack=True)channel.start_consuming()

0 0