python 消息队列如何接收处理_python使用消息队列RabbitMq(进阶)
importpika
connection=pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel=connection.channel()#声明queue
channel.queue_declare(queue='hello')#RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')print("[x] Sent 'Hello World!'")
connection.close()
发送
__author__ = 'hardy'
importpika
connection=pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel=connection.channel()#You may ask why we declare the queue again ‒ we have already declared it in our previous code.#We could avoid that if we were sure that the queue already exists. For example if send.py program#was run before. But we're not yet sure which program to run first. In such cases it's a good#practice to repeat declaring the queue in both programs.
channel.queue_declare(queue='hello')defcallback(ch, method, properties, body):print("[x] Received %r" %body)
channel.basic_consume(callback,
queue='hello',
no_ack=True)print('[*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
接收
消息队列的发送端流程
1、连接
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
2、声明queue
channel.queue_declare(queue='hello')
队列持久化
channel.queue_declare(queue='hello', durable=True)
3、发送消息
channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
消息持久化(必须队列持久化)
channel.basic_publish(exchange='',
routing_key="hello",
body=message,
properties=pika.BasicProperties(
delivery_mode = 2, # make message persistent
))
4、关闭
connection.close()
消息队列接收端流程
1、连接
connection = pika.BlockingConnection(pika.ConnectionParameters(
'localhost'))
channel = connection.channel()
2、声明queue
channel.queue_declare(queue='hello')
3、创建回调函数(处理数据)
def callback(ch, method, properties, body):
print(" [x] Received %r" % body)
4、设置
channel.basic_consume(callback,
queue='hello',
no_ack=True)
5、开始接收数据
channel.start_consuming()
6、确认消息被消费
def callback(ch, method, properties, body):
print(" [x] Received %r" % body)
time.sleep(body.count(b'.'))
print(" [x] Done")
ch.basic_ack(delivery_tag = method.delivery_tag)
channel.basic_consume(callback,
queue='task_queue',
no_ack=True #no_ack=True消息不需要确认,默认no_ack=false,消息需要确认
)
python 消息队列如何接收处理_python使用消息队列RabbitMq(进阶)相关推荐
- python数据库抓取并保存_python:微信消息抓取、转发和数据库存储及源码
前言 python的强大在于丰富的类库,经常会看到几行代码就可以实现非常强大的功能.它可以做爬虫.AI.自动化测试.小工具(抢票.抓包.微信消息抓取)等等. 本次我们来讲讲怎么来抓取微信消息?抓取微信 ...
- Linux学习笔记-消息队列的接收
目录 接收消息 举个例子 接收消息 #include <sys/msg.h> ssize_t msgrcv(int msgqid, void *ptr, size_t nbytes, lo ...
- python消息队列_python之消息队列
消息持久化 acknowledgment 消息不丢失的方法 生效方法:channel.basic_consume(consumer_callback, queue, no_ack=False, exc ...
- python消息队列celery高可用_分布式消息队列-Celery
怎么能不恨呢,在我发现自己是恶鬼的时候,在我最绝望最虚弱的时候,这个世上最该跟我在一起的人却用刀把我的心刺穿了 Celery 是 Distributed Task Queue,分布式任务队列.分布式决 ...
- python中队列的应用用场景_消息队列应用场景
原文http://blog.csdn.net/konglongaa/article/details/52208273 一.消息队列概述 消息队列中间件是分布式系统中重要的组件,主要解决应用解耦,异步消 ...
- python中队列的应用用场景_消息队列使用的四种场景介绍
四.JMS消息服务 讲消息队列就不得不提JMS .JMS(JavaMessage Service,Java消息服务)API是一个消息服务的标准/规范,允许应用程序组件基于JavaEE平台创建.发送.接 ...
- python多线程_Python多线程和队列结合demo
每周一07:22,准时为你充电 一.使用场景 大家都知道python的多线程不是真正的多线程,但是对于io类型的任务,多线程还是能发挥作用的.那么多个线程之间是如何进行变量共享的呢,很多时候我们可以借 ...
- python 网络编程之Socket通信案例消息发送与接收
背景 网络编程是python编程中的一项基本技术.本文将实现一个简单的Socket通信案例消息发送与接收 正文 在python中的socket编程的大致流程图如上所示 我们来首先编写客户端的代码: # ...
- linux消息通信无法接收,进程间通信:消息队列有关问题:进程1接收不到进程2的消息...
进程间通信:消息队列有关问题:进程1接收不到进程2的消息 进程间通信:消息队列有关问题:进程1接收不到进程2的消息 日期:2014-05-16 浏览次数:20365 次 进程间通信:消息队列问题:进程 ...
最新文章
- 忽略这一点,人工智能变人工智障!
- 智能家居成新热点 市场化进程遇瓶颈
- python 给字典排序
- tt桌球瞄准器手机版_新年换台新手机!各价位段哪些机型值得入手?
- 一个没法商用,但是好玩有趣的 Python 手绘图形库!
- Codeforces 666E. Forensic Examination
- 嵌入式电路设计(电路仿真)
- 【Silverlight】Bing Maps学习系列(二):通过Bing Maps Silverlight Control如何显示地图...
- 播放量子计算机的声音,首次实现:用声音纠缠了两个量子比特!
- Python分布式爬虫打造搜索引擎(四)
- Android之多种Bitmap效果
- 刷题记录 kuangbin带你飞专题六:最小生成树
- linux rhel 7系统启动过程
- python共享单车数据分析_共享单车数据可视化分析(Python/Seaborn)
- f1签证计算机专业容易拒签吗,美国f1签证会被拒签吗?
- Windows10 最新版官方iso镜像(截至2021/02)win10 2009(20h2)
- Servlet内存马
- 源支付聚合免签支付系统
- mongodb被锁定 --repair
- Solidity基础入门讲解