Rabbitmq -Routeing模式- python编码实现

(using the pika 0.10.0 Python client)

In the previous tutorial we built a simple logging system. We were able to broadcast log messages to many receivers.

In this tutorial we‘re going to add a feature to it - we‘re going to make it possible to subscribe only to a subset of the messages. For example, we will be able to direct only critical error messages to the log file (to save disk space), while still being able to print all of the log messages on the console.

# 以上均是客套话,哥自己看的懂就算了,不给翻译了,节省时间,哈哈。

Bindings

In previous examples we were already creating bindings. You may recall code like:

channel.queue_bind(exchange=exchange_name,queue=queue_name)

A binding is a relationship between an exchange and a queue. This can be simply read as: the queue is interested in messages from this exchange.

Bindings can take an extra routing_key parameter. To avoid the confusion with a basic_publishparameter we‘re going to call it a binding key. This is how we could create a binding with a key:

# 简单的翻译下:绑定是exchange和队列之间的一种关系,这可以简单的理解为: 这个队列对来自于exchange的信息非常感兴趣,绑定需要采取一个额外的参数routing_key , 为了避免这个有关于basic_publish 这个参数的困惑,我们准备称呼它为binding key,这就是我们如何用一个钥匙创建一个绑定

channel.queue_bind(exchange=exchange_name,queue=queue_name,routing_key=‘black‘)

The meaning of a binding key depends on the exchange type. The fanout exchanges, which we used previously, simply ignored its value.

#translate: 这个意思是说 绑定钥匙取决于exchange的类型,这个扇出exchange,是我们以前使用过的,简单的忽略其值

Direct exchange

Our logging system from the previous tutorial broadcasts all messages to all consumers. We want to extend that to allow filtering messages based on their severity. For example we may want the script which is writing log messages to the disk to only receive critical errors, and not waste disk space on warning or info log messages.

We were using a fanout exchange, which doesn‘t give us too much flexibility - it‘s only capable of mindless broadcasting.

We will use a direct exchange instead. The routing algorithm behind a direct exchange is simple - a message goes to the queues whose binding key exactly matches the routing keyof the message.

# translate:我们的日志系统从前面的教程里面可知会广播信息到所有的消费者,我们想扩展它,允许基于信息的严重性来过滤一些信息,例如我们可以想要这个脚本只把接收到的极重要的错误信息写入磁盘,不想把磁盘的空间浪费在警告或者提示信息里面

我们使用扇出交换器,这个扇出exchange不会给我们太多的灵活性,它,它只能怪盲目的广播。

我们也会使用直连交换器去替代扇出exchange,这个直连exchange背后的路由算法更加简单,一条信息能准确匹配到到绑定了key的队列里面。

To illustrate that, consider the following setup:

In this setup, we can see the direct exchange X with two queues bound to it. The first queue is bound with binding key orange, and the second has two bindings, one with binding key blackand the other one with green.

In such a setup a message published to the exchange with a routing key orange will be routed to queue Q1. Messages with a routing key of black or green will go to Q2. All other messages will be discarded.

# translate: 在这一步,我们能够看到这个直连交换器x被两个队列所束缚住,这个第一个队列是束缚住了orange这个key,第二个有两个绑定,一个绑定的key是blackend,另一个key是green

Emitting logs

We‘ll use this model for our logging system. Instead of fanout we‘ll send messages to a directexchange. We will supply the log severity as a routing key. That way the receiving script will be able to select the severity it wants to receive. Let‘s focus on emitting logs first.

# translate: 我们为我们的日志系统使用这个模型,我们发送信息到直连exchange上而不是扇出exchange,我们能够提供这个日志严重程度对于routing key,在这种方式下接收脚本能够去选择他想要接受的严重性,首先让我们集中精力在这发送日志脚本上

Like always we need to create an exchange first:

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

 And we‘re ready to send a message:

channel.basic_publish(exchange=‘direct_logs‘,routing_key=severity,body=message)

To simplify things we will assume that ‘severity‘ can be one of ‘info‘, ‘warning‘, ‘error‘.

Subscribing

Receiving messages will work just like in the previous tutorial, with one exception - we‘re going to create a new binding for each severity we‘re interested in.

result=channel.queue_declare(exclusive=True)queue_name=result.method.queueforseverityinseverities:channel.queue_bind(exchange=‘direct_logs‘,queue=queue_name,routing_key=severity)

 

Putting it all together

时间: 2024-09-30 03:31:43

Rabbitmq -Routeing模式- python编码实现的相关文章

Rabbitmq -Publish_Subscribe模式- python编码实现

what is Exchanges ?? Let's quickly go over what we covered in the previous tutorials: A producer is a user application that sends messages. A queue is a buffer that stores messages. A consumer is a user application that receives messages. The core id

使用rabbitmq rpc 模式

服务器端 安装 ubuntu 16.04 server 安装 rabbitmq-server 设置 apt 源 curl -s https://packagecloud.io/install/repositories/rabbitmq/rabbitmq-server/script.python.sh | bash 使用 apt-get install rabbitmq-server 安装 rabbitmq 服务器 按键Y或者 y 确认安装 rabbitmq-server 简单管理 rabbitm

VS2013+PTVS,python编码问题

1.调试,input('中文'),乱码2.调试,print('中文'),正常3.不调试,input('中文'),正常4.不调试,print('中文'),正常 页面编码方式已经加了"# -- coding:utf-8 --" 这是调试模式下的截图: VS2013+PTVS,python编码问题 >> python 这个答案描述的挺清楚的:http://www.goodpm.net/postreply/python/1010000008988526/VS2013PTVSpyth

python编码问题,从隐隐作痛到除去病根

查阅的资料链接 python编码为什么这么蛋疼 python2.7手册str函数 python源文件默认编码与内部默认编码 1.源文件默认编码为ASCII,所以,如果不显示声明当前代码用什么编码写的,python会用ASCII去解析,如果源文件中有UTF-8编码,由于ASCII不能翻译UTF8编码,则会报错了. #file test.py 使用UTF8保存 a='a' b='好' 运行后 SyntaxError: Non-ASCII character '\xe5' in file test.p

PYTHON编码处理-str与Unicode的区别

一篇关于str和Unicode的好文章 整理下python编码相关的内容 注意: 以下讨论为Python2.x版本, Py3k的待尝试 开始 用python处理中文时,读取文件或消息,http参数等等 一运行,发现乱码(字符串处理,读写文件,print) 然后,大多数人的做法是,调用encode/decode进行调试,并没有明确思考为何出现乱码 所以调试时最常出现的错误 错误1 Traceback (most recent call last): File "<stdin>"

Python编码规则

1. 命名规则 1.1 变量名.包名.模块名 变量名通常有字母.数字和下划线组成,且首字母必须是字母或下划线,并且不能使用python的保留字:包名.模块名通常用小写字母 1.2 类名.对象名 类名首字母用大写,其他字母采用小写:对象名用小写字母.类的属性和方法名以对象作为前缀,对象通过操作符"."访问属性和方法.类的私有变量.私有方法以两个下划线作为前缀. l.3 函数名     函数名通常采用小写,并用下划线或单词首字母大写来增加名称的可读性,导入的函数以模块名作为前缀. 2. 模

Python 编码

Python 编码 ASCII.Unicode.UTF-8 以及 gbk 在具体说明 Python 编码之前,先来理清 ASCII.Unicode.UTF-8.gbk 究竟是什么? 这边仅简单介绍下,具体请百度. ASCII:是现今最通用的单字节编码系统.ASCII(仅1~127) 仅可代表英文.数字及一些符号等,如,A 的 ASCII 码为65(十进制). Unicode:为了解决传统的字符编码方案的局限而产生,为每种语言中的每个字符设定了统一并且唯一的二进制编码,以满足跨语言.跨平台进行文本

说说Python编码规范

前言 已有近两个月没有发表过文章了,前段时间外甥和女儿过来这边渡暑假,平常晚上和周末时间都陪着她们了,趁这个周末有空,再抽空再把这块拾起来.         这么久没写了,再次拿起键盘,想想,发表些什么呢,想起上次公司的代码评审委员会下周其中一个议题是关于Python编码规范的整理,那就趁热打铁,整理一份关于Python编码规范的文章,也为那些写Python的人,提供一些编码注意的一些事项或者说是参考吧. 编码规范的作用         规范故明思义,就是通过不断的总结,吸取好的点,从而形成的一

python 编码问题:&#39;ascii&#39; codec can&#39;t encode characters in position 的解决方案

问题描述: Python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报这样的错UnicodeDecodeError: 'ascii' codec can't decode byte 0x?? in position 1: ordinal not in range(128),python没办法处理非ascii编码的,此时需要自己设置将python的默认编码,一般设置为utf8的编码格式. 查询系统默认编码可以在解释器中输入以下命令: Python代码