Python操作rabbitMQ的示例代码


Posted in Python onMarch 19, 2019

引入

RabbitMQ 是一个由 Erlang 语言开发的 AMQP 的开源实现。

rabbitMQ是一款基于AMQP协议的消息中间件,它能够在应用之间提供可靠的消息传输。在易用性,扩展性,高可用性上表现优秀。使用消息中间件利于应用之间的解耦,生产者(客户端)无需知道消费者(服务端)的存在。而且两端可以使用不同的语言编写,大大提供了灵活性。

Python操作rabbitMQ的示例代码

中文文档

安装

# 安装配置epel源
  rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
 
# 安装erlang
  yum -y install erlang
 
# 安装RabbitMQ
  yum -y install rabbitmq-server

# 启动/停止
  service rabbitmq-server start/stop

rabbitMQ工作模型

简单模式

生产者

import pika
connection = pika.BlockingConnection(pika.ConnectionParameters( host='localhost'))

channel = connection.channel()

channel.queue_declare(queue='hello')

channel.basic_publish(exchange='',
           routing_key='hello',
           body='Hello World!')

print(" [x] Sent 'Hello World!'")
connection.close()

消费者

connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
 
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,no-ack = False

如果消费者遇到情况(its channel is closed, connection is closed, or TCP connection is lost)挂掉了,那么,RabbitMQ会重新将该任务添加到队列中。

  • 回调函数中的 ch.basic_ack(delivery_tag=method.delivery_tag)
  • basic_comsume中的no_ack=False

接收消息端应该这么写:

?
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
    host='10.211.55.4'))
channel = connection.channel()

channel.queue_declare(queue='hello')

def callback(ch, method, properties, body):
  print(" [x] Received %r" % body)
  import time
  time.sleep(10)
  print 'ok'
  ch.basic_ack(delivery_tag = method.delivery_tag)

channel.basic_consume(callback,
           queue='hello',
           no_ack=False)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()

2,durable :消息不丢失

生产者

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.211.55.4'))
channel = connection.channel()

# make message persistent
channel.queue_declare(queue='hello', durable=True)

channel.basic_publish(exchange='',
           routing_key='hello',
           body='Hello World!',
           properties=pika.BasicProperties(
             delivery_mode=2, # make message persistent
           ))
print(" [x] Sent 'Hello World!'")
connection.close()

3,消息获取顺序

默认消息队列里的数据是按照顺序被消费者拿走,例如:消费者1 去队列中获取 奇数 序列的任务,消费者1去队列中获取 偶数 序列的任务。

channel.basic_qos(prefetch_count=1) 表示谁来谁取,不再按照奇偶数排列

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.211.55.4'))
channel = connection.channel()

# make message persistent
channel.queue_declare(queue='hello')


def callback(ch, method, properties, body):
  print(" [x] Received %r" % body)
  import time
  time.sleep(10)
  print 'ok'
  ch.basic_ack(delivery_tag = method.delivery_tag)

channel.basic_qos(prefetch_count=1)

channel.basic_consume(callback,
           queue='hello',
           no_ack=False)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()

exchange模型

1,发布订阅

Python操作rabbitMQ的示例代码

发布订阅和简单的消息队列区别在于,发布订阅会将消息发送给所有的订阅者,而消息队列中的数据被消费一次便消失。所以,RabbitMQ实现发布和订阅时,会为每一个订阅者创建一个队列,而发布者发布消息时,会将消息放置在所有相关队列中。

exchange type = fanout

生产者

import pika
import sys

connection = pika.BlockingConnection(pika.ConnectionParameters(
    host='localhost'))
channel = connection.channel()

channel.exchange_declare(exchange='logs',
             type='fanout')

message = ' '.join(sys.argv[1:]) or "info: Hello World!"
channel.basic_publish(exchange='logs',
           routing_key='',
           body=message)
print(" [x] Sent %r" % message)
connection.close()

消费者

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
    host='localhost'))
channel = connection.channel()

channel.exchange_declare(exchange='logs',
             type='fanout')

result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue

channel.queue_bind(exchange='logs',
          queue=queue_name)

print(' [*] Waiting for logs. To exit press CTRL+C')

def callback(ch, method, properties, body):
  print(" [x] %r" % body)

channel.basic_consume(callback,
           queue=queue_name,
           no_ack=True)

channel.start_consuming()

2,关键字发送

Python操作rabbitMQ的示例代码

之前事例,发送消息时明确指定某个队列并向其中发送消息,RabbitMQ还支持根据关键字发送,即:队列绑定关键字,发送者将数据根据关键字发送到消息exchange,exchange根据 关键字 判定应该将数据发送至指定队列。

exchange type = direct

import pika
import sys

connection = pika.BlockingConnection(pika.ConnectionParameters(
    host='localhost'))
channel = connection.channel()

channel.exchange_declare(exchange='direct_logs',
             type='direct')

result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue

severities = sys.argv[1:]
if not severities:
  sys.stderr.write("Usage: %s [info] [warning] [error]\n" % sys.argv[0])
  sys.exit(1)

for severity in severities:
  channel.queue_bind(exchange='direct_logs',
            queue=queue_name,
            routing_key=severity)

print(' [*] Waiting for logs. To exit press CTRL+C')

def callback(ch, method, properties, body):
  print(" [x] %r:%r" % (method.routing_key, body))

channel.basic_consume(callback,
           queue=queue_name,
           no_ack=True)

channel.start_consuming()

3,模糊匹配

Python操作rabbitMQ的示例代码

exchange type = topic

发送者路由值 队列中
old.boy.python old.* -- 不匹配
old.boy.python old.# -- 匹配

在topic类型下,可以让队列绑定几个模糊的关键字,之后发送者将数据发送到exchange,exchange将传入”路由值“和 ”关键字“进行匹配,匹配成功,则将数据发送到指定队列。

  • # 表示可以匹配 0 个 或 多个 单词
  • *  表示只能匹配 一个 单词
import pika
import sys

connection = pika.BlockingConnection(pika.ConnectionParameters(
    host='localhost'))
channel = connection.channel()

channel.exchange_declare(exchange='topic_logs',
             type='topic')

result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue

binding_keys = sys.argv[1:]
if not binding_keys:
  sys.stderr.write("Usage: %s [binding_key]...\n" % sys.argv[0])
  sys.exit(1)

for binding_key in binding_keys:
  channel.queue_bind(exchange='topic_logs',
            queue=queue_name,
            routing_key=binding_key)

print(' [*] Waiting for logs. To exit press CTRL+C')

def callback(ch, method, properties, body):
  print(" [x] %r:%r" % (method.routing_key, body))

channel.basic_consume(callback,
           queue=queue_name,
           no_ack=True)

channel.start_consuming()

基于rabbitMQ的RPC

 Callback queue 回调队列

一个客户端向服务器发送请求,服务器端处理请求后,将其处理结果保存在一个存储体中。而客户端为了获得处理结果,那么客户在向服务器发送请求时,同时发送一个回调队列地址 reply_to

Correlation id 关联标识

一个客户端可能会发送多个请求给服务器,当服务器处理完后,客户端无法辨别在回调队列中的响应具体和那个请求时对应的。为了处理这种情况,客户端在发送每个请求时,同时会附带一个独有 correlation_id 属性,这样客户端在回调队列中根据 correlation_id 字段的值就可以分辨此响应属于哪个请求。

客户端发送请求:

某个应用将请求信息交给客户端,然后客户端发送RPC请求,在发送RPC请求到RPC请求队列时,客户端至少发送带有reply_to以及correlation_id两个属性的信息

服务端工作流:

等待接受客户端发来RPC请求,当请求出现的时候,服务器从RPC请求队列中取出请求,然后处理后,将响应发送到reply_to指定的回调队列中

客户端接受处理结果:

客户端等待回调队列中出现响应,当响应出现时,它会根据响应中correlation_id字段的值,将其返回给对应的应用

服务者

import pika

# 建立连接,服务器地址为localhost,可指定ip地址
connection = pika.BlockingConnection(pika.ConnectionParameters(
    host='localhost'))

# 建立会话
channel = connection.channel()

# 声明RPC请求队列
channel.queue_declare(queue='rpc_queue')

# 数据处理方法
def fib(n):
  if n == 0:
    return 0
  elif n == 1:
    return 1
  else:
    return fib(n-1) + fib(n-2)

# 对RPC请求队列中的请求进行处理
def on_request(ch, method, props, body):
  n = int(body)

  print(" [.] fib(%s)" % n)

  # 调用数据处理方法
  response = fib(n)

  # 将处理结果(响应)发送到回调队列
  ch.basic_publish(exchange='',
           routing_key=props.reply_to,
           properties=pika.BasicProperties(correlation_id = \
                             props.correlation_id),
           body=str(response))
  ch.basic_ack(delivery_tag = method.delivery_tag)

# 负载均衡,同一时刻发送给该服务器的请求不超过一个
channel.basic_qos(prefetch_count=1)

channel.basic_consume(on_request, queue='rpc_queue')

print(" [x] Awaiting RPC requests")
channel.start_consuming()

客户端

import pika
import uuid

class FibonacciRpcClient(object):
  def __init__(self):
    """
    客户端启动时,创建回调队列,会开启会话用于发送RPC请求以及接受响应
    """
    # 建立连接,指定服务器的ip地址
    self.connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='localhost'))
        
    # 建立一个会话,每个channel代表一个会话任务
    self.channel = self.connection.channel()
    
    # 声明回调队列,再次声明的原因是,服务器和客户端可能先后开启,该声明是幂等的,多次声明,但只生效一次
    result = self.channel.queue_declare(exclusive=True)
    # 将次队列指定为当前客户端的回调队列
    self.callback_queue = result.method.queue
    
    # 客户端订阅回调队列,当回调队列中有响应时,调用`on_response`方法对响应进行处理; 
    self.channel.basic_consume(self.on_response, no_ack=True,
                  queue=self.callback_queue)


  # 对回调队列中的响应进行处理的函数
  def on_response(self, ch, method, props, body):
    if self.corr_id == props.correlation_id:
      self.response = body


  # 发出RPC请求
  def call(self, n):
  
    # 初始化 response
    self.response = None
    
    #生成correlation_id 
    self.corr_id = str(uuid.uuid4())
    
    # 发送RPC请求内容到RPC请求队列`rpc_queue`,同时发送的还有`reply_to`和`correlation_id`
    self.channel.basic_publish(exchange='',
                  routing_key='rpc_queue',
                  properties=pika.BasicProperties(
                     reply_to = self.callback_queue,
                     correlation_id = self.corr_id,
                     ),
                  body=str(n))
                  
    
    while self.response is None:
      self.connection.process_data_events()
    return int(self.response)

# 建立客户端
fibonacci_rpc = FibonacciRpcClient()

# 发送RPC请求
print(" [x] Requesting fib(30)")
response = fibonacci_rpc.call(30)
print(" [.] Got %r" % response)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
使用Python脚本在Linux下实现部分Bash Shell的教程
Apr 17 Python
Python3.x爬虫下载网页图片的实例讲解
May 22 Python
Python使用pandas处理CSV文件的实例讲解
Jun 22 Python
python 3.6.4 安装配置方法图文教程
Sep 18 Python
浅谈Pandas:Series和DataFrame间的算术元素
Dec 22 Python
Python设计模式之桥接模式原理与用法实例分析
Jan 10 Python
对python读取zip压缩文件里面的csv数据实例详解
Feb 08 Python
Python3.0中普通方法、类方法和静态方法的比较
May 03 Python
Python爬取网页信息的示例
Sep 24 Python
python 爬虫请求模块requests详解
Dec 04 Python
Python线程池与GIL全局锁实现抽奖小案例
Apr 13 Python
Python 图片添加美颜效果
Apr 28 Python
Python Matplotlib实现三维数据的散点图绘制
Mar 19 #Python
浅谈python中get pass用法
Mar 19 #Python
使用matplotlib中scatter方法画散点图
Mar 19 #Python
详解django+django-celery+celery的整合实战
Mar 19 #Python
详解Python正则表达式re模块
Mar 19 #Python
python matplotlib画图库学习绘制常用的图
Mar 19 #Python
详解python的四种内置数据结构
Mar 19 #Python
You might like
PHP操作mysql数据库分表的方法
2016/06/09 PHP
Javascript 模式实例 观察者模式
2009/10/24 Javascript
JavaScript 判断判断某个对象是Object还是一个Array
2010/01/28 Javascript
JavaScript中OnLoad几种使用方法
2012/12/15 Javascript
6款经典实用的jQuery小插件及源码(对话框/提示工具等等)
2013/02/04 Javascript
jQuery中Form相关知识汇总
2015/01/06 Javascript
深入理解JavaScript系列(34):设计模式之命令模式详解
2015/03/03 Javascript
easyui window refresh 刷新两次的解决方法(推荐)
2016/05/18 Javascript
Node.js+Express配置入门教程详解
2016/05/19 Javascript
json定义及jquery操作json的方法
2016/09/29 Javascript
完美解决IE不支持Data.parse()的问题
2016/11/24 Javascript
详解基于javascript实现的苹果系统底部菜单
2016/12/02 Javascript
angularjs2 ng2 密码隐藏显示的实例代码
2017/08/01 Javascript
JavaScript实现的浏览器下载文件的方法
2017/08/09 Javascript
浅谈React的最大亮点之虚拟DOM
2018/05/29 Javascript
详解vue开发中调用微信jssdk的问题
2019/04/16 Javascript
浅谈小程序globalData的那些事儿
2019/11/01 Javascript
js实现菜单跳转效果
2020/12/11 Javascript
[01:46]DOTA2上海特锦赛小组赛英文解说KotlGuy采访
2016/02/27 DOTA
[58:00]DOTA2-DPC中国联赛 正赛 PSG.LGD vs Elephant BO3 第二场 2月7日
2021/03/11 DOTA
使用anaconda的pip安装第三方python包的操作步骤
2018/06/11 Python
OpenCV图像颜色反转算法详解
2019/05/13 Python
python设置环境变量的作用和实例
2019/07/09 Python
python在OpenCV里实现投影变换效果
2019/08/30 Python
Python 中pandas索引切片读取数据缺失数据处理问题
2019/10/09 Python
numpy的Fancy Indexing和array比较详解
2020/06/11 Python
Python venv虚拟环境配置过程解析
2020/07/08 Python
CSS3教程:background-clip和background-origin
2008/10/17 HTML / CSS
HTML5调用手机发短信和打电话功能
2020/04/29 HTML / CSS
国家励志奖学金个人先进事迹材料
2014/05/04 职场文书
区级文明单位申报材料
2014/05/15 职场文书
商场开业庆典策划方案
2014/06/02 职场文书
教师节感谢信
2015/01/22 职场文书
工作调动申请报告
2015/05/18 职场文书
致三级跳运动员加油稿
2015/07/21 职场文书
曾国藩励志经典名言37句,蕴含哲理
2019/10/14 职场文书