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(进阶)的更多相关文章

  1. python使用消息队列RabbitMq(入门)

    windows平台开发和使用 安装 安装Erlang:https://pan.baidu.com/s/1QcZDaI205uaue7mMWh5cSA 安装RabbitMQ:https://pan.ba ...

  2. 消息队列rabbitmq/kafka

    12.1 rabbitMQ 1. 你了解的消息队列 rabbitmq是一个消息代理,它接收和转发消息,可以理解为是生活的邮局.你可以将邮件放在邮箱里,你可以确定有邮递员会发送邮件给收件人.概括:rab ...

  3. 消息队列rabbitmq rabbitMQ安装

    消息队列rabbitmq   12.1 rabbitMQ 1. 你了解的消息队列 生活里的消息队列,如同邮局的邮箱, 如果没邮箱的话, 邮件必须找到邮件那个人,递给他,才玩完成,那这个任务会处理的很麻 ...

  4. openstack (共享服务) 消息队列rabbitmq服务

    云计算openstack共享组件——消息队列rabbitmq(3)   一.MQ 全称为 Message Queue, 消息队列( MQ ) 是一种应用程序对应用程序的通信方法.应用程序通过读写出入队 ...

  5. C#中使用消息队列RabbitMQ

    在C#中使用消息队列RabbitMQ 2014-10-27 14:41 by qy1141, 745 阅读, 2 评论, 收藏, 编辑 1.什么是RabbitMQ.详见 http://www.rabb ...

  6. node使用消息队列RabbitMQ一

    基础发布和订阅 消息队列RabbitMQ使用 1 安装RabbitMQ服务器 安装erlang服务 下载地址 http://www.erlang.org/downloads 安装RabbitMQ 下载 ...

  7. 消息队列--RabbitMQ(一)

    1.消息队列概述 可以理解为保存消息的一个媒介/或者是个容器,与之相关有两个概念(即生产者(Publish)与消费者(Consumer)).所谓生产者,就是生产创造消息的一方,那么,消费者便是从队列中 ...

  8. (二)RabbitMQ消息队列-RabbitMQ消息队列架构与基本概念

    原文:(二)RabbitMQ消息队列-RabbitMQ消息队列架构与基本概念 没错我还是没有讲怎么安装和写一个HelloWord,不过快了,这一章我们先了解下RabbitMQ的基本概念. Rabbit ...

  9. (一)RabbitMQ消息队列-RabbitMQ的优劣势及产生背景

    原文:(一)RabbitMQ消息队列-RabbitMQ的优劣势及产生背景 本篇并没有直接讲到技术,例如没有先写个Helloword.我想在选择了解或者学习一门技术之前先要明白为什么要现在这个技术而不是 ...

随机推荐

  1. [Codeforces 163D]Large Refrigerator (DFS+剪枝)

    [Codeforces 163D]Large Refrigerator (DFS+剪枝) 题面 已知一个长方体的体积为V,三边长a,b,c均为正整数,求长方体的最小表面积S V以质因数分解的形式给出 ...

  2. [fw]linux 下如何查看和踢除正在登陆的其它用户

    linux 下如何查看和踢除正在登陆的其它用户 Posted on 2011/09/01   如何在linux下查看当前登录的用户,并且踢掉你认为应该踢掉的用户?请使用who这个命令来查看当前正在登录 ...

  3. elasticsearch 基础 —— Inner hits

    Inner hits The parent-join and nested 功能允许返回具有不同范围匹配的文档.在父/子案例中,基于子文档中的匹配返回父文档,或者基于父文档中的匹配返回子文档.在嵌套的 ...

  4. elk系统生成请求数据测试承载量、宕机瓶颈shell

    elk-gen-data.sh: #!/usr/bin/bash#----------------------------------------------------# Comment: to g ...

  5. 条款2:尽量使用const ,enum,inline替换define

    宁可使用编译器而不用预处理器 假设我们使用预处理器: #define ABC 1.56 这标识符ABC也许编译器没看到,也许它在编译器处理源码前就被预处理器移走了,于是“标识符”ABC没有进入标识符列 ...

  6. 前端学习(三十七)angular(笔记)

    MVC     后台    M         Module             数据层    V         View             视图层    C         Contro ...

  7. SAP固定资产(FI-AA),一网打尽(转)

    在固定资产模块的边缘,游荡了四五年的时间了,细细数来,固定资产这个熟悉又抽象的东西(熟悉是由于天天接触常常听说,抽象是具体到系统实现上有些复杂并且常遇到问题),好像也没有多少东西. 1) SAP固定资 ...

  8. 接口需要上一个接口的返回值(unittest)

    import unittest,requests ''' 使用unittest框架的时候,这个接口需要上一个接口的返回值 ''' class Test_case(unittest.TestCase): ...

  9. Redis服务器中有75%受到恶意软件感染

    尽管由于配置错误的服务器和应用程序而导致新的网络攻击不断出现,但人们仍然忽略安全警告. 近两个月前,中国知名黑客组织东方联盟研究人员警告说,一项针对开放Redis服务器的大规模恶意软件活动现在已经发展 ...

  10. BZOJ3331 BZOJ2013 压力

    考前挣扎 圆方树这么早就出现了嘛... 要求每个点必须被经过的次数 所以就是路径上的割点/端点++ 由于圆方树上所有非叶子圆点都是割点 所以就是树上差分就可以辣. 实现的时候出了一点小问题. 就是这里 ...