pika

pika处理消息可以简单分为以下几个步骤:

  1. 我们首先创建连接对象,然后启动事件循环。
  2. 当有连接时,调用on_connected方法。在该方法中创建channel
  3. channel创建完成,将调用on_channel_open方法。在该方法中,声明了一个queue。
  4. queue声明成功后,将调用on_queue_declared。在该方法中,调用channel.basic_consume,为RabbitMQ传递的每条消息调用handle_delivery。
  5. 当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种连接器

  1. pika.BlockingConnection - 同步模式, 简单易用
  2. pika.SelectConnection - 没有第三方依赖包的异步模式
  3. pika.adapters.tornado_connection.TornadoConnection - 基于Tornado 的异步IO请求模式
  4. pika.adapters.twisted_connection.TwistedProtocolConnection - 基于Twisted’的异步IO请求模式
 

pika详解 (一)的更多相关文章

  1. pika详解(四) channel 通道

    pika详解(四) channel 通道   本文链接:https://blog.csdn.net/comprel/article/details/94662394 版权 ​ channel通道 通道 ...

  2. pika详解(五)登录认证及connectionParameters

    pika详解(五)登录认证及connectionParameters 本文链接:https://blog.csdn.net/comprel/article/details/94662916 版权 pi ...

  3. pika详解(三)SelectConnection及其他Connection

    pika详解(三)SelectConnection及其他Connection   本文链接:https://blog.csdn.net/comprel/article/details/94661147 ...

  4. pika详解(二) BlockingConnection

    pika详解(二) BlockingConnection   本文链接:https://blog.csdn.net/comprel/article/details/94592348 版权 Blocki ...

  5. Linq之旅:Linq入门详解(Linq to Objects)

    示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...

  6. 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)

    一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...

  7. EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解

    前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...

  8. Java 字符串格式化详解

    Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...

  9. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

随机推荐

  1. Ionic5沉浸式状态栏 适配全面屏

    1. 在platforms/android/app/src/main目录中找到AndroidManifest.xml文件,修改文件中manifest → application → activity标 ...

  2. 适用于分布式ID的雪花算法

    基于Java实现的适用于分布式ID的雪花算法工具类,这里存一下日后好找 /** * 雪花算法生成ID */ public class SnowFlakeUtil { private final sta ...

  3. 使用Viper读取Nacos配置(开源)

    使用Viper读取Nacos配置(开源) 一.前言 目前Viper支持的Remote远程读取配置如 etcd, consul:目前还没有对Nacos进行支持,本文中将开源一个Nacos的Viper支持 ...

  4. Vue.js小案例、生命周期函数及axios的使用

    一.调色框小案例: 随着三个滑动框的变化,颜色框的颜色随之改变 1.1.实例代码 <!DOCTYPE html> <html lang="en" xmlns:v- ...

  5. Sql server注入一些tips

    sql server环境测试: 几个特性: 1.sql server兼容性可以说是最差的. 举例: select x from y where id=1 字符串查询 select x from y w ...

  6. 分布式任务调度系统:xxl-job

    任务调度,通俗来说实际上就是"定时任务",分布式任务调度系统,翻译一下就是"分布式环境下定时任务系统". xxl-job一个分布式任务调度平台,其核心设计目标是 ...

  7. PHP--date转成时间戳,time()获取的是秒数

    date("Y-m-d H:i:s");  //如果存成datetime型在MYSQL中 必须用这种格式 实现功能:获取某个日期的时间戳,或获取某个时间的时间戳.strtotime ...

  8. php正则表达式过滤空格 换行符 回车

    我整理了几个比较适合的实例了,对于它们我们是有很多站长都测试过并用过了,不过文章最后我的总结也是生重要的哦,至于原因我也说不上了,因为chr是ascii编码了所以有时浏览器会自动转成ascii,特别像 ...

  9. 病毒木马查杀实战第010篇:QQ盗号木马之十六进制代码分析

    前言 按照我的个人习惯,在运用诸如IDA Pro与OllyDBG对病毒进行逆向分析之前,我都会利用一些自动化的工具,通过静态或动态的分析方法(参见<病毒木马查杀第008篇:熊猫烧香之病毒查杀总结 ...

  10. Windows Pe 第三章 PE头文件(中)

    这一章的上半部分大体介绍了下PE文件头,下半部分是详细介绍里面的内容,这一章一定要多读几遍,好好记记基础概念和知识,方便之后的学习. 简单回忆一下: 3.4  PE文件头部解析 3.4.1 DOS M ...