rabbitmq的笔记(三)用Python生产和消费
2021/8/26 17:06:10
本文主要是介绍rabbitmq的笔记(三)用Python生产和消费,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
消费者: #!/usr/local/bin/python2.7 # -*- coding: utf-8 -*- import pika connection = pika.BlockingConnection(pika.ConnectionParameters('172.16.148.79', 5672, '/')) channel = connection.channel() def callback(channel, method, properties, message): print("get resultc from queue %s" % message) channel.basic_consume('rhj', callback,) channel.start_consuming() 生产者: #!/usr/local/bin/python2.7 # -*- coding: utf-8 -*- import pika connection = pika.BlockingConnection(pika.ConnectionParameters('172.16.148.78', 5672,'/')) channel = connection.channel() channel.queue_declare(queue='rhj') channel.basic_publish(exchange='', routing_key='rhj', body='rabbitmq003') connection.close() 先执行消费者,会一直监听队列。等执行生产者,消息马上被消费,显示在消费者那里。 这样的程序适合在新安装的rabbitmq和各种迁移升级里面把读写过程展示出来,可以体现在变更的过程中有没有消息丢失了。 如果遇到集群,要填写所有节点的读写,这一在参数的一栏这样填写。 集群的写法: Parameters = ( pika.ConnectionParameters('39.104.65.201', 5672,'/',credentials = credentials,connection_attempts=5, retry_delay=1), pika.ConnectionParameters('39.99.35.129', 5672,'/',credentials = credentials,connection_attempts=5, retry_delay=1), pika.ConnectionParameters('39.104.24.129', 5672,'/',credentials = credentials,connection_attempts=5, retry_delay=1)) connection = pika.BlockingConnection(Parameters)这篇关于rabbitmq的笔记(三)用Python生产和消费的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门
- 2024-11-14Python编程入门指南
- 2024-11-13Python基础教程
- 2024-11-12Python编程基础指南
- 2024-11-12Python基础编程教程
- 2024-11-08Python编程基础与实践示例
- 2024-11-07Python编程基础指南