1. pika.PlainCredentials(username, password, erase_on_connect)

  1. 功能:创建连接时的登录凭证
  2. 参数:
  • username: MQ 账号
  • password: MQ 密码
  • erase_on_connect: 删除连接上的凭据, 默认为 False
credentials = pika.PlainCredentials(username="guest", password="guest")

2. pika.ConnectionParameters(host, port, virtual_host, credentials)

  1. 功能: 连接 MQ 的参数设置
  2. 参数:
  • host: RabbitMQ IP 地址
  • port: RabbitMQ 端口
  • virtual_host: RabbitMQ 虚拟主机
  • credentials: 登录凭证
pika.ConnectionParameters(host='127.0.0.1', port=5672, virtual_host='/', credentials=credentials)

3. pika.BlockingConnection(parameters)

  1. 功能: 阻塞式连接 MQ
  2. 参数:
  • parameters: 连接参数(包含主机/端口/虚拟主机/账号/密码等凭证信息)
connect = pika.BlockingConnection(pika.ConnectionParameters(host='127.0.0.1', port=5672, virtual_host='/', credentials=credentials))

4. pika.channel(channel_number)

  1. 功能: 创建信道
  2. 参数:
  • channel_number: 信道个数, 一般采用默认值 None
channel = connect.channel()

5. channel.exchange_declare(callback,exchange,exchange_type,passive,durable,auto_delete,internal,nowait,arguments)

  1. 功能: 声明交换器
  2. 参数:
  • callback=None : 当 Exchange.DeclareOk 时 调用该方法, 当 nowait=True 该值必须为 None
  • exchange=None: 交换器名称,保持非空,由字母、数字、连字符、下划线、句号组成
  • exchange_type=‘direct’: 交换器类型
  • passive=False: 执行一个声明或检查它是否存在
  • durable=False: RabbitMQ 重启时保持该交换器的持久性,即不会丢失
  • auto_delete=False: 没有队列绑定到该交换器时,自动删除该交换器
  • internal=False: 只能由其它交换器发布-Can only be published to by other exchanges
  • nowait=False: 不需要 Exchange.DeclareOk 的响应-Do not expect an Exchange.DeclareOk response
  • arguments=None: 对该交换器自定义的键/值对, 默认为空
channel.exchange_declare(exchange='hello', exchange_type='direct', passive=False, durable=True, auto_delete=False)

6. channel.queue_declare(callback,queue,passive,durable,exclusive,auto_delete,nowait,arguments)

  1. 功能: 声明队列
  2. 参数:
  • callback : 当 Queue.DeclareOk 时的回调方法; 当 nowait=True 时必须为 None.
  • queue=’’ : 队列名称
  • passive=False : 只检查队列是否存在
  • durable=False : 当 RabbitMQ 重启时,队列保持持久性
  • exclusive=False : 仅仅允许当前的连接访问
  • auto_delete=False : 当消费者取消或者断开连接时, 自动删除该队列
  • nowait=False : 当 Queue.DeclareOk 时不需要等待
  • arguments=None : 对该队列自定义键/值对
channel.queue_declare(queue='hello')

7. channel.queue_bind(callback, queue, exchange,routing_key,nowait,arguments)

  1. 功能: 通过路由键将队列和交换器绑定
  2. 参数:
  • callback: 当 Queue.BindOk 时的回调函数, 当 nowait=True 时必须为 None
  • queue: 要绑定到交换器的队列名称
  • exchange: 要绑定的源交换器
  • routing_key=None: 绑定的路由键
  • nowait=False: 不需要 Queue.BindOk 的响应
  • arguments=None: 对该绑定自定义键/值对
channel.queue_bind(queue='hello', exchange='hello', routing_key='world')

8. channel.basic_publish(exchange, routing_key, body, properties, mandatory, immediate)

  1. 功能: 将消息发布到 RabbitMQ 交换器上
  2. 参数:
  • exchange: 要发布的目标交换器
  • routing_key: 该交换器所绑定的路由键
  • body: 携带的消息主体
  • properties=None: 消息的属性,即文本/二进制等等
  • mandatory=False: 当 mandatory 参数设置为 true 时,交换机无法根据自身的路由键找到一个符合的队列,
    那么 RabbitMQ 会调用 Basic.Return 命令将消息返回给生产者,
    当 mandatory 参数设置为 false 时,出现上述情况,消息会被丢弃
  • immediate=False: 立即性标志
channel.basic_publish(exchange='hello',  routing_key='world',  properties=msg_props, body=message)

9. channel.basic_consume(consumer_callback, queue, no_ack, exclusive, consumer_tag, arguments)

  1. 功能: 从队列中拿到消息开始消费
  2. 参数:
  • consumer_callback:
    先来看官方的解释
The function to call when consuming with the signature
consumer_callback(channel, method, properties,body),wherechannel: pika.Channelmethod: pika.spec.Basic.Deliverproperties: pika.spec.BasicPropertiesbody: str, unicode, or bytes (python 3.x)

意思是说,当要消费时,调用该回调函数 consumer_callback, 函数的参数有channel, method, properties,body

  • queue=’’: 要消费的消息队列
  • no_ack=False: 自动确认已经消费成功
  • exclusive=False: 不允许其它的消费者消费该队列
  • consumer_tag=None: 指定自己的消费标记
  • arguments=None: 对该消费者自定义设置键值对
def callback(channel, method, properties, body):# 消息确认channel.basic_ack(delivery_tag=method.delivery_tag)if body.decode('utf-8') == "quit":# 停止消费,并退出channel.basic_cancel(consumer_tag='hello-consumer')channel.close()connect.close()else:print("msg is {}".format(body))channel.basic_consume(callback, queue='hello', no_ack=False)

10. channel.basic_ack()

  1. 功能: 消息确认
  2. 参数:
  • delivery_tag=0 : 服务端分配的传递标识
  • multiple=False:

If set to True, the delivery tag is treated as “up to and including”, so that multiple messages can be acknowledged with a single method. If set to False, the delivery tag refers to a single message. If the multiple field is 1, and the delivery tag is zero, this indicates acknowledgement of all outstanding messages.

channel.basic_ack(delivery_tag=method.delivery_tag)

11. channel.basic_cancel(callback, consumer_tag, nowait)

  1. 功能: 取消消费, 该方法不会影响已经发送的消息,但是不会再发送新的消息给消费者
  2. 参数:
  • callback=None : 当 Basic.CancelOk 响应时的回调函数; 当 nowait=True 时必须为 None. 当 nowait=False 时必须是可回调的函数
  • consumer_tag=’’: 消费标识
  • nowait=False : 不期望得到 Basic.CancelOk response

If set to True, the delivery tag is treated as “up to and including”, so that multiple messages can be acknowledged with a single method. If set to False, the delivery tag refers to a single message. If the multiple field is 1, and the delivery tag is zero, this indicates acknowledgement of all outstanding messages.

channel.basic_cancel(consumer_tag='hello-consumer')

12. channel.start_consuming()

  1. 功能: 处理 I/O 事件和 basic_consume 的回调, 直到所有的消费者被取消
  2. 参数:
    NA
channel.start_consuming()

13. channel.basic_reject(delivery_tag, requeue=True)

  1. 功能: 拒绝单条消息
  2. 参数:
  • delivery_tag : 传递标签
  • requeue=True : 是否重新放回到队列中去
channel.basic_reject()

14. channel.basic_nack(delivery_tag=None, multiple=False, requeue=True)

  1. 功能: 拒绝单条或者多条消息
  2. 参数:
  • delivery_tag=None : 传递标签
  • multiple=False : 是否批量,即多条消息

If set to True, the delivery tag is treated as “up to and including”, so that multiple messages can be acknowledged with a single method. If set to False, the delivery tag refers to a single message. If the multiple field is 1, and the delivery tag is zero, this indicates acknowledgement of all outstanding messages.

  • requeue=True: 是否重新放回到队列中去
channel.basic_nack()

14. channel.queue_delete(callback=None,queue=’’,if_unused=False,if_empty=False,nowait=False)

  1. 功能: 删除已声明的队列
  2. 参数:
  • callback=None: The callback to call on Queue.DeleteOk; MUST be None when nowait=True
  • queue=’’: The queue to delete
  • if_unused=False: only delete if it’s unused
  • if_empty=False: only delete if the queue is empty
  • nowait=False: Do not wait for a Queue.DeleteOk
channel.queue_delete()

15. channel.exchange_delete(callback=None,exchange=None,if_unused=False,nowait=False)

  1. 功能: 删除已声明的交换器
  2. 参数:
  • callback=None: The function to call on Exchange.DeleteOk; MUST be None when nowait=True.
  • exchange=None: The exchange name
  • if_unused=False: only delete if the exchange is unused
  • nowait=False: Do not wait for an Exchange.DeleteOk
channel.exchange_delete()

16. pika.BasicProperties()

  1. 功能: 发送消息的属性
  2. 参数:
  • content_type=None,
  • content_encoding=None,
  • headers=None,
  • delivery_mode=None, 声明信息持久化, 使信息持久化,需要声明queue持久化和delivery_mode=2信息持久化
  • priority=None,
  • correlation_id=None,
  • reply_to=None,
  • expiration=None,
  • message_id=None,
  • timestamp=None,
  • type=None,
  • user_id=None,
  • app_id=None,
  • cluster_id=None
msg_props = pika.BasicProperties()
msg_props.content_type = 'text/plain'

17. 回调函数 callback

  1. 功能: 回调函数
  2. 参数:
  • channel: 包含channel的一切属性和方法
  • method: 包含 consumer_tag, delivery_tag, exchange, redelivered, routing_key
  • properties: basic_publish 通过 properties 传入的参数
  • body: basic_publish发送的消息
def callback(channel, method, properties, body):# 消息确认channel.basic_ack(delivery_tag=method.delivery_tag)if body.decode('utf-8') == "quit":# 停止消费,并退出channel.basic_cancel(consumer_tag='hello-consumer')channel.close()connect.close()else:print("msg is {}".format(body))

RabbitMQ 入门系列(9)— Python 的 pika 库常用函数及参数说明相关推荐

  1. Python之Pandas库常用函数大全(含注释)

    前言:本博文摘抄自中国慕课大学上的课程<Python数据分析与展示>,推荐刚入门的同学去学习,这是非常好的入门视频. 继续一个新的库,Pandas库.Pandas库围绕Series类型和D ...

  2. Python之Matplotlib库常用函数大全(含注释)

    前言:本博文摘抄自中国慕课大学上的课程<Python数据分析与展示>,推荐刚入门的同学去学习,这是非常好的入门视频. plt.savefig('test', dpi = 600) :将绘制 ...

  3. Python中turtle库常用函数

    记录turtle库中经常用到的函数. turtle.forward(distance)  画笔向前移动distance距离 turtle.backforward(distance)  画笔向后移动di ...

  4. RabbitMQ 入门系列(10)— RabbitMQ 消息持久化、不丢失消息

    消息要保持"持久化",即不丢失,必须要使得消息.交换器.队列,必须全部 "持久化". 1. 生产者怎么确认 RabbitMQ 已经收到了消息? # 打开通道的确 ...

  5. python采用pika库使用rabbitmq总结,多篇笔记和示例

    这一段时间学习了下rabbitmq,在学习的过程中,发现国内关于python采用pika库使用rabbitmq的资料很少,官网有这方面的资料,不过是都英文的.于是笔者结合自己的理解,就这方面内容写了一 ...

  6. python安装扩展库常用的工具是什么和conda_python习题01——董付国学习系列

    一.简答题 1.python程序的__name__的作用是什么? 每一个python程序都有一个__name__变量,用来表示程序的运行方式,当作为模块导入时,__name__变量的值等于程序文件名, ...

  7. python安装扩展库常用_树Python安装扩展库常用的是()工具

    树Python安装扩展库常用的是()工具 创业的要素主要包括:①创业者②资金③项目④场地A:错B:对 边缘检测最通用的方法是检测亮度值的不连续性,这样的不连续是用一阶和二阶导数检测的.A:对B:错 由 ...

  8. Python中numpy.linalg库常用函数

    Python中numpy.linalg库常用函数 numpy.linalg Python中numpy.linalg库常用函数 简单记录所遇到的numpy库内置函数 矩阵与向量积 ①np.linalg. ...

  9. python安装扩展库常用的是什么工具

    pip 是 Python 包管理工具,该工具提供了对Python 包的查找.下载.安装.卸载的功能. 目前如果你在 http://python.org 下载最新版本的安装包,则是已经自带了该工具. P ...

最新文章

  1. 联想家庭云中心:天边飘来“故乡的云”
  2. Ubuntu Qt编译报错 stdlib.h: No such file or directory
  3. mysql 多行拼接注入_MySQL注入汇总
  4. css3中animation动画、浏览器私有前缀、文字阴影
  5. leetcode —— 31. 下一个排列
  6. Memory Management Concepts overview(内存管理基本概念)
  7. Oracle数据库块的物理损坏与逻辑损坏
  8. 2021牛客暑期多校训练营6,签到题CFHI
  9. 开发安全的ASP.NET应用程序
  10. VM上安装Redhat无法选包
  11. hadoop配置历史服务器
  12. 详解数据治理体系(值得收藏)
  13. python 安装 pyHook
  14. xml转json(使用工具)
  15. java 时区 夏令时_关于时区:如何显示Java开始夏令时后的时间差异?
  16. 这个神器:功能强大的 Mac 剪切板记录管理工具
  17. nfs共享存储web项目集群的一次下载流为空FIleChannel空洞bug
  18. 2021-2027全球与中国大客户营销软件市场现状及未来发展趋势
  19. 贵阳骑龙村:一根小香葱闯出大市场
  20. 《测量助理》最新版本V3.0.221215发布更新

热门文章

  1. 使用hello word写小说
  2. Linux/docker下oracle开启监听,开启自动启动
  3. GCC 编译 C++ 程序分步骤流程(预处理 gcc -E、编译 gcc -S、汇编 gcc -c 和链接 gcc 以及 gcc -o 选项)
  4. c语言使用指定字符串替换特定的子串
  5. debian10 简单的CA使用
  6. linux各种模式切换
  7. ELECTRA 超过bert预训练NLP模型
  8. tensorflow学习笔记(四十五):sess.run(tf.global_variables_initializer()) 做了什么?
  9. LeetCode简单题之杨辉三角 II
  10. LLVM Clang前端编译与调试