python使用消息队列RabbitMq(进阶)
import pika 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'
import pika 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') def callback(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使用消息队列RabbitMq(进阶)的更多相关文章
- python使用消息队列RabbitMq(入门)
windows平台开发和使用 安装 安装Erlang:https://pan.baidu.com/s/1QcZDaI205uaue7mMWh5cSA 安装RabbitMQ:https://pan.ba ...
- 消息队列rabbitmq/kafka
12.1 rabbitMQ 1. 你了解的消息队列 rabbitmq是一个消息代理,它接收和转发消息,可以理解为是生活的邮局.你可以将邮件放在邮箱里,你可以确定有邮递员会发送邮件给收件人.概括:rab ...
- 消息队列rabbitmq rabbitMQ安装
消息队列rabbitmq 12.1 rabbitMQ 1. 你了解的消息队列 生活里的消息队列,如同邮局的邮箱, 如果没邮箱的话, 邮件必须找到邮件那个人,递给他,才玩完成,那这个任务会处理的很麻 ...
- openstack (共享服务) 消息队列rabbitmq服务
云计算openstack共享组件——消息队列rabbitmq(3) 一.MQ 全称为 Message Queue, 消息队列( MQ ) 是一种应用程序对应用程序的通信方法.应用程序通过读写出入队 ...
- C#中使用消息队列RabbitMQ
在C#中使用消息队列RabbitMQ 2014-10-27 14:41 by qy1141, 745 阅读, 2 评论, 收藏, 编辑 1.什么是RabbitMQ.详见 http://www.rabb ...
- node使用消息队列RabbitMQ一
基础发布和订阅 消息队列RabbitMQ使用 1 安装RabbitMQ服务器 安装erlang服务 下载地址 http://www.erlang.org/downloads 安装RabbitMQ 下载 ...
- 消息队列--RabbitMQ(一)
1.消息队列概述 可以理解为保存消息的一个媒介/或者是个容器,与之相关有两个概念(即生产者(Publish)与消费者(Consumer)).所谓生产者,就是生产创造消息的一方,那么,消费者便是从队列中 ...
- (二)RabbitMQ消息队列-RabbitMQ消息队列架构与基本概念
原文:(二)RabbitMQ消息队列-RabbitMQ消息队列架构与基本概念 没错我还是没有讲怎么安装和写一个HelloWord,不过快了,这一章我们先了解下RabbitMQ的基本概念. Rabbit ...
- (一)RabbitMQ消息队列-RabbitMQ的优劣势及产生背景
原文:(一)RabbitMQ消息队列-RabbitMQ的优劣势及产生背景 本篇并没有直接讲到技术,例如没有先写个Helloword.我想在选择了解或者学习一门技术之前先要明白为什么要现在这个技术而不是 ...
随机推荐
- centos 7.2 查看时间,精确到毫秒级别
[root@ ~]# date +'%x %X.%N' 2019年08月06日 11时25分13秒.193666438 [root@commonTest ~]# date --help 用法:date ...
- 左侧菜单收缩的实现(包括,筛选器,addclass、removeclass、绑定事件,链式编程)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- upx压缩notepad.exe(运行时压缩)
PEView:https://www.lanzous.com/i5k9vbg UPX:https://www.lanzous.com/i5k9vch notepad.exe:https://www.l ...
- 四、局域网连接SqlServer
一.局域网连接SqlServer 一台服务器上装有四个数据库的时候,我们可以通过IP\实例名的方式进行访问. navicat 连接sqlserver数据库
- Git 常用命令简单记录
分布式版本控制系统,跟踪文本文件的改动 ubuntu安装: sudo apt install git 安装完成后,设置使用的用户名和邮箱: 全局: git config --global user.n ...
- AndroidStudio之Theme、colorPrimary、colorPrimaryDark、colorAccent详解
今天就来看看在Androi5.0中常用的颜色属性. 我们可以先定义一个style,然后在这个style中设定每一个Activity或者整个App的颜色,最后在清单文件中来给某个Activity设置主题 ...
- OpenStack虚拟机网络问题
当发现你的OpenStack虚拟机网络有问题,不妨先试一下这16个步骤 1. Security Group全部打开,这是最基本的,但是很多人容易忘记 其实遇到过无数这种场景了,Debug了半天网络 ...
- activemq---点对点/发布订阅模式简单代码示例
activemq 消息模式流程: ConnnectionFactory --> Connection --> Session --> Message ---ConnectionFac ...
- setInterval,setTimeout,clearInterval
定时器 var i=0; function iadd(){ i++; console.log(i) } setInterval(iadd,1000);//1.2.3... 超时调用 var i=0; ...
- nyoj 1022:合纵连横(并查集删点)
题目链接 参考链接 只附代码好了 #include<bits/stdc++.h> using namespace std; ; int a[N],b[N],vis[N]; int n,m, ...