pika详解 (一)
pika详解 (一)
pika
pika处理消息可以简单分为以下几个步骤:
- 我们首先创建连接对象,然后启动事件循环。
- 当有连接时,调用on_connected方法。在该方法中创建channel
- channel创建完成,将调用on_channel_open方法。在该方法中,声明了一个queue。
- queue声明成功后,将调用on_queue_declared。在该方法中,调用channel.basic_consume,为RabbitMQ传递的每条消息调用handle_delivery。
- 当RabbitMQ有发送消息,将调用handle_delivery方法传递AMQP Method框架,Header框架和Body
官方给出的示例如下:
import pika
# Create a global channel variable to hold our channel object in
channel = None
# Step #2
def on_connected(connection):
"""Called when we are fully connected to RabbitMQ"""
# Open a channel
connection.channel(on_channel_open)
# Step #3
def on_channel_open(new_channel):
"""Called when our channel has opened"""
global channel
channel = new_channel
channel.queue_declare(queue="test", durable=True, exclusive=False, auto_delete=False, callback=on_queue_declared)
# Step #4
def on_queue_declared(frame):
"""Called when RabbitMQ has told us our Queue has been declared, frame is the response from RabbitMQ"""
channel.basic_consume('test', handle_delivery)
# Step #5
def handle_delivery(channel, method, header, body):
"""Called when we receive a message from RabbitMQ"""
print(body)
# Step #1: Connect to RabbitMQ using the default parameters
parameters = pika.ConnectionParameters()
connection = pika.SelectConnection(parameters, on_connected)
try:
# Loop so we can communicate with RabbitMQ
connection.ioloop.start()
except KeyboardInterrupt:
# Gracefully close the connection
connection.close()
# Loop until we're fully closed, will stop on its own
connection.ioloop.start()
认证:
认证使用PlainCredentials ,传递给credentials参数
import pika
credentials = pika.PlainCredentials('username', 'password')
parameters = pika.ConnectionParameters(credentials=credentials)
连接器的参数传递:
有两种方式:ConnectionParameters 和 URLParameters
TCP Backpressure
TCP背压, 流控机制的一种, 在rabbitmq 2.0 channel.flow移除,使用tcp backpresssure进行限流。 参数backpressure_detection。 如果pika发现有太多消息积压, 将调用通过add_backpressure_callback注册的回调函数。
默认超过平常10倍的积压将会调用,这个参数也可以设置, 设置方法为set_backpressure_multiplier 值为整数
例如:
import pika
parameters = pika.URLParameters('amqp://guest:guest@rabbit-server1:5672/%2F?backpressure_detection=t')
pika扩展时, 需要启动监听,使用connection.ioloop.start()方法
import pika
def on_open(connection):
# Invoked when the connection is open
pass
# Create our connection object, passing in the on_open method
connection = pika.SelectConnection(on_open_callback=on_open)
try:
# Loop so we can communicate with RabbitMQ
connection.ioloop.start()
except KeyboardInterrupt:
# Gracefully close the connection
connection.close()
# Loop until we're fully closed, will stop on its own
connection.ioloop.start()
拥有4种连接器
- pika.BlockingConnection - 同步模式, 简单易用
- pika.SelectConnection - 没有第三方依赖包的异步模式
- pika.adapters.tornado_connection.TornadoConnection - 基于Tornado 的异步IO请求模式
- pika.adapters.twisted_connection.TwistedProtocolConnection - 基于Twisted’的异步IO请求模式
pika详解 (一)的更多相关文章
- pika详解(四) channel 通道
pika详解(四) channel 通道 本文链接:https://blog.csdn.net/comprel/article/details/94662394 版权 channel通道 通道 ...
- pika详解(五)登录认证及connectionParameters
pika详解(五)登录认证及connectionParameters 本文链接:https://blog.csdn.net/comprel/article/details/94662916 版权 pi ...
- pika详解(三)SelectConnection及其他Connection
pika详解(三)SelectConnection及其他Connection 本文链接:https://blog.csdn.net/comprel/article/details/94661147 ...
- pika详解(二) BlockingConnection
pika详解(二) BlockingConnection 本文链接:https://blog.csdn.net/comprel/article/details/94592348 版权 Blocki ...
- Linq之旅:Linq入门详解(Linq to Objects)
示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...
- 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)
一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...
- EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解
前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...
- Java 字符串格式化详解
Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...
- Android Notification 详解(一)——基本操作
Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...
随机推荐
- 【全网首发】鸿蒙开源三方组件--强大的弹窗库XPopup组件
目录: 1.介绍 2.效果一览 3.依赖 4.如何使用 5.下载链接 6.<鸿蒙开源三方组件>文章合集 1. 介绍 XPopup是一个弹窗库,可能是Harmony平台最好的弹窗库.它从 ...
- IPC 方法分类
IPC 方法分类 进程间通信 shell out 被调用程序在执行完毕之前接管用户的键盘和显示,退出后,调用程序重新控制键盘和显示并继续运行. 专门程序通常有文件系统与父进程进行通信,方法是在指定位置 ...
- C#-获取CPUID
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- Git 无法添加文件夹下的文件
尚未暂存以备提交的变更: (使用 "git add <文件>..." 更新要提交的内容) (使用 "git checkout -- <文件>... ...
- python中的的异步IO
asyncio 是干什么的? 异步网络操作 并发 协程 python3.0 时代,标准库里的异步网络模块:select(非常底层) python3.0时代,第三方异步网络库:Tornado pytho ...
- 10.qml-组件、Loader、Component介绍
1.组件介绍 一个组件通常由一个qml文件定义(单独文件定义组件), 实际也可以在qml里面通过Component对象来嵌入式定义组件 (4小节讲解). Component对象封装的内容默认不会显示, ...
- python爬虫——汽车之家数据
相信很多买车的朋友,首先会在网上查资料,对比车型价格等,首选就是"汽车之家",于是,今天我就给大家扒一扒汽车之家的数据: 一.汽车价格: 首先获取的数据是各款汽车名称.价格范围以及 ...
- Scrum Meeting 3
Basic Info where:三号教学楼 when:2020/4/27 target: 简要汇报一下已完成任务,下一步计划与遇到的问题 Progress Team Member Position ...
- MzzTxx——团队贡献分分配方案
项目 内容 这个作业属于哪个课程 2021春季计算机学院软件工程(罗杰 任健) 这个作业的要求在哪里 团队贡献分分配规则制定 我在这个课程的目标是 进一步提升工程化开发能力,积累团队协作经验,熟悉全栈 ...
- 【软工】个人项目作业——个人软件流程(PSP)
[软工]个人项目作业--个人软件流程(PSP) 项目 内容 班级:北航2020春软件工程 006班(罗杰.任健 周五) 博客园班级博客 作业:设计程序求几何对象的交点集合 个人项目作业 个人课程目标 ...