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 文中如有纰 ...
随机推荐
- 注册中心与API网关不是这样用的!
之前在做顾问和咨询项目的时候,见到了一种非常经典的关于API网关和注册中心的错误用法.这个案例在我的星球里已经分享过,没想到最近又碰到了两个类似的使用姿势.也许这样的问题还存在不少团队的应用中,所以拿 ...
- 基于MVC框架的JavaWeb网站开发demo项目(JSP+Servlet+JavaBean)
1.环境配置 Windows10+Eclipse2020+jdk8+Tomcat9+MySQL8+Navicat10 2.需求分析 ①用户登录注册注销(查找.增加) ②显示用户列表(查找) ③显示用户 ...
- 1-3 Postman 注册账号与登录
1.为什么要注册postman账号 注册postman账号是免费的.当注册并登陆一个postman账号后,用户可以获得如下权限. (1)同步和备份历史,集合,环境,和预置头. (2)可以轻松的处理来自 ...
- win 远程桌面 ubuntu
开始 起因 因为工作需求经常要远程 局域网内的 Ubuntu设备 之前用的一直是 Teamviver 但是最近不知怎么了,Teamviver发病 打不开了,也懒得折腾了! 简单的记录一下 能用的几种连 ...
- Dockerfile多阶段构建
多阶段构建 之前的做法: 在Docker17.05版本之前,构建Docker镜像,通常采用两种方式: 1.全部放入一个Dockerfile 一种方式是将所有的构建过程全都包含在一个Dockerfile ...
- SSM整合大体步骤
SSM整合步骤: 1. 导入jar spring: springMVC: mybatis: 第三方支持:log4j,pageHelper,AspectJ,jackson,jstl 2. 搭建sprin ...
- Sublime text3 的破解
下载sublimeText3的安装包并安装(已经安装的可以忽略) 在hosts文件中添加:127.0.0.1 license.sublimehq.com(hosts文件地址:C:\Windows\Sy ...
- controller通过map返回减少dto类的创建
更多精彩关注公众号 不要把实体类对象直接返给前端 ,首先想到的是创建DTO,但是这样就造成大量的DTO,显得很臃肿,为了减少dto的数量,像一些比较少的参数避免创建不必要的DTO,通过本次优化达到业务 ...
- vscode 超好用的前端插件
一 vscode 前端调试接口的插件 作为前端工程师,接口的调试是我们必不可少的工作.以前用过postman,但是作为一个vscode重度使用者,我希望看看vscode能否进行对接口的调试.省的跟后台 ...
- Redis数据结构—跳跃表
目录 Redis数据结构-跳跃表 跳跃表产生的背景 跳跃表的结构 利用跳跃表查询有序链表 Redis跳跃表图示 Redis跳跃表数据结构 小结 Redis数据结构-跳跃表 大家好,我是白泽,最近学校有 ...