Publish / Subscribe
发布/订阅

redis-py includes a PubSub object that subscribes to channels and listens for new messages. Creating a PubSub object is easy.
/redis-py 带有可以订阅频道又可以聆听新信息的 PubSub 目标。创建一个PubSub的方法如下:
>>> r = redis.StrictRedis(...)
>>> p = r.pubsub()

Once a PubSub instance is created, channels and patterns can be subscribed to.
创建PubSub 实例后就可以聆听频道和模式了。
>>> p.subscribe('my-first-channel', 'my-second-channel', ...)
>>> p.psubscribe('my-*', ...)

The PubSub instance is now subscribed to those channels/patterns. The subscription confirmations can be seen by reading messages from the PubSub instance.
现在这个PubSub实力已经订阅了这些频道/模式。通过读取来自PubSub实例的信息可以确定订阅已经成功。
>>> p.get_message()
{'pattern': None, 'type': 'subscribe', 'channel': 'my-second-channel', 'data': 1L}
>>> p.get_message()
{'pattern': None, 'type': 'subscribe', 'channel': 'my-first-channel', 'data': 2L}
>>> p.get_message()
{'pattern': None, 'type': 'psubscribe', 'channel': 'my-*', 'data': 3L}

Every message read from a PubSub instance will be a dictionary with the following keys.
所有通过PubSub实例读取的信息都是一个带有以下钥匙的字典:
type: One of the following: 'subscribe', 'unsubscribe', 'psubscribe', 'punsubscribe', 'message', 'pmessage'
类型:下面其中之一:。。。
channel: The channel [un]subscribed to or the channel a message was published to
频道:信息发布给订阅或者被订阅的频道
pattern: The pattern that matched a published message's channel. Will be None in all cases except for 'pmessage' types.
模式:一种对应(发布信息的频道)的模式。除了‘pmessage’类型都是’None’。
data: The message data. With [un]subscribe messages, this value will be the number of channels and patterns the connection is currently subscribed to. With [p]message messages, this value will be the actual published message.
数据:信息里的数据。带有订阅或者未订阅的信息,这个钥匙值代表本次连接被订阅的频道或模式的数量。带有[p]message信息时,这个钥匙值代表实际发布的信息。

Let's send a message now.
现在来发送一个信息。
# the publish method returns the number matching channel and pattern
# subscriptions.
发布的方法返回对应频道和模式的订阅的数量
'my-first-channel' matches both the 'my-first-channel'
# subscription and the 'my-*' pattern subscription, so this message will
# be delivered to 2 channels/patterns
'my-first-channel'对应'my-first-channel’的订阅和'my-*’模式的订阅,所以这个信息会被传送到两个频道/模式>>> r.publish('my-first-channel', 'some data')
2
>>> p.get_message()
{'channel': 'my-first-channel', 'data': 'some data', 'pattern': None, 'type': 'message'}
>>> p.get_message()
{'channel': 'my-first-channel', 'data': 'some data', 'pattern': 'my-*', 'type': 'pmessage'}

Unsubscribing works just like subscribing. If no arguments are passed to [p]unsubscribe, all channels or patterns will be unsubscribed from.
取消订阅和订阅是一样的道理。如果没有参数传到[p]unsubscribe,所有的频道和模式都会被取消订阅。
>>> p.unsubscribe()
>>> p.punsubscribe('my-*')
>>> p.get_message()
{'channel': 'my-second-channel', 'data': 2L, 'pattern': None, 'type': 'unsubscribe'}
>>> p.get_message()
{'channel': 'my-first-channel', 'data': 1L, 'pattern': None, 'type': 'unsubscribe'}
>>> p.get_message()
{'channel': 'my-*', 'data': 0L, 'pattern': None, 'type': 'punsubscribe'}

redis-py also allows you to register callback functions to handle published messages. Message handlers take a single argument, the message, which is a dictionary just like the examples above. To subscribe to a channel or pattern with a message handler, pass the channel or pattern name as a keyword argument with its value being the callback function.
redis-py还可以允许你组册回调函数去处理发布的信息。信息处理者会接受一个参数,信息,也就是和上面说的字典例子一样。当想要订阅一个带有信息处理者的频道或者模式时,传送以频道或者模式的名字为关键字的参数,参数的值为回调函数名。

When a message is read on a channel or pattern with a message handler, the message dictionary is created and passed to the message handler. In this case, a None value is returned from get_message() since the message was already handled.
当信息被频道或模式的信息处理者读取时,信息的字典会被创造然后传送给信息处理者。在这个情况下,没有数值会被get_message()传回,因为信息已经被信息处理者处理了。
>>> def my_handler(message):
... print 'MY HANDLER: ', message['data']
>>> p.subscribe(**{'my-channel': my_handler})
# read the subscribe confirmation message
读取订阅确认信息
>>> p.get_message()
{'pattern': None, 'type': 'subscribe', 'channel': 'my-channel', 'data': 1L}
>>> r.publish('my-channel', 'awesome data')
1
# for the message handler to work, we need tell the instance to read data.
# this can be done in several ways (read more below). we'll just use
# the familiar get_message() function for now
为了让信息处理者工作,我们必须让这个实例去读取数据。
有几种方法可以做到这点(更多的内容下面会介绍)。
这里我们用熟悉的get_message()函数
>>> message = p.get_message()
MY HANDLER: awesome data
# note here that the my_handler callback printed the string above.
# `message` is None because the message was handled by our handler.
注意这里回调函数my_handler打印出的字符串
`message`为空,因为信息被我们的信息处理者处理了>>> print message
None

If your application is not interested in the (sometimes noisy) subscribe/unsubscribe confirmation messages, you can ignore them by passing ignore_subscribe_messages=True to r.pubsub(). This will cause all subscribe/unsubscribe messages to be read, but they won't bubble up to your application.
如果你的应用不需要订阅/取消订阅的确认信息(有时候会很吵),你可以忽略他们:传送ignore_subscribe_messages=True到r.pubsub(),这会使所有的订阅/取消订阅信息被读取,但是他们不会充满你的应用。
>>> p = r.pubsub(ignore_subscribe_messages=True)
>>> p.subscribe('my-channel')
>>> p.get_message() # hides the subscribe message and returns None
>>> r.publish('my-channel')
1
>>> p.get_message()
{'channel': 'my-channel', data': 'my data', 'pattern': None, 'type': 'message'}

There are three different strategies for reading messages.
下面介绍几种读取信息的方法。

The examples above have been using pubsub.get_message(). Behind the scenes, get_message() uses the system's 'select' module to quickly poll the connection's socket. If there's data available to be read, get_message() will read it, format the message and return it or pass it to a message handler. If there's no data to be read, get_message() will immediately return None. This makes it trivial to integrate into an existing event loop inside your application.
刚才的例子使用了pubsub.get_message(),实际上,get_message()使用的是系用的'select’模块来快速检测(poll)连接的socket。如果有数据可以读取,get_message()会读取它们,格式好这些信息然后传给信息处理者。如果没有数据可以读取,则马上返回None。这种方法是比较直接的融入到你的应用的事件循环中。
>>> while True:
>>> message = p.get_message()
>>> if message:
>>> # do something with the message
>>> time.sleep(0.001) # be nice to the system :)

Older versions of redis-py only read messages with pubsub.listen(). listen() is a generator that blocks until a message is available. If your application doesn't need to do anything else but receive and act on messages received from redis, listen() is an easy way to get up an running.
更老版本的redis-py只会用pubsub.listen()读取信息。listen()是一个发射器,只有信息存在时才会运作。如果的应用除了接收然后处理来自redis的信息之外,不需要其他的东西的话,listen()是一个比较简便运作方法。
>>> for message in p.listen():
... # do something with the message

The third option runs an event loop in a separate thread. pubsub.run_in_thread() creates a new thread and starts the event loop. The thread object is returned to the caller of run_in_thread(). The caller can use the thread.stop() method to shut down the event loop and thread. Behind the scenes, this is simply a wrapper around get_message() that runs in a separate thread, essentially creating a tiny non-blocking event loop for you. run_in_thread() takes an optional sleep_time argument. If specified, the event loop will call time.sleep() with the value in each iteration of the loop.
第三种方法是在另外一个线程运行一个事件循环。pubsub.run_in_thread()会创建一个新的线程然后开始事件循环。这个线程目标会返回给run_in_thread()的召唤者。这个召唤者可以用thread.stop()方法来停止事件循环和线程。实际上,这是一个运行在另一个线程的简单的get_message()包装,本质上是创建了一个小的不阻挡的事件循环。run_in_thread()还可以接受一个选择性的sleep_time参数。如果注明了,这个事件循环会召唤time.sleep()然后给出每一个循环的碎叫时间。

Note: Since we're running in a separate thread, there's no way to handle messages that aren't automatically handled with registered message handlers. Therefore, redis-py prevents you from calling run_in_thread() if you're subscribed to patterns or channels that don't have message handlers attached.
注意,因为我们是在另一个线程运行,所以没有办法来处理那些(没有自动被(组册的信息处理者)处理过的)信息。所以redis-py不允许你召唤run_in_thread()如果你订阅了一个没有信息处理者的模式或频道。
>>> p.subscribe(**{'my-channel': my_handler})
>>> thread = p.run_in_thread(sleep_time=0.001)
# the event loop is now running in the background processing messages
# when it's time to shut it down...
现在这个事件循环正在后台处理信息
当需要关闭它时。。。
>>> thread.stop()

A PubSub object adheres to the same encoding semantics as the client instance it was created from. Any channel or pattern that's unicode will be encoded using the charset specified on the client before being sent to Redis. If the client's decode_responses flag is set the False (the default), the 'channel', 'pattern' and 'data' values in message dictionaries will be byte strings (str on Python 2, bytes on Python 3). If the client's decode_responses is True, then the 'channel', 'pattern' and 'data' values will be
automatically decoded to unicode strings using the client's charset.
一个PubSub目标遵循和客户实例同样的编码格式。在被发送给Redis之前,任何unicode的频道或者模式会被编码成由可以的charset指明的编码方式。如果客户的decode_responses flag被设置成默认的False,'channel', 'pattern' and ‘data’里的信息字典的值会是byte 字符串。(str on Python 2, bytes on Python 3).
如果客户的client's decode_responses是True,则自动解码为客户的charset。

PubSub objects remember what channels and patterns they are subscribed to. In the event of a disconnection such as a network error or timeout, the PubSub object will re-subscribe to all prior channels and patterns when reconnecting. Messages that were published while the client was disconnected cannot be delivered. When you're finished with a PubSub object, call its .close() method to shutdown the connection.
PubSub目标会记住他订阅的频道或者模式。在因为网络故障或者暂停而发生连接断开时,他会在重新连接之前再一次订阅之前的模式和频道。在连接断开时发布的信息不会被送达。当你使用完PubSub目标时,召唤它的.close()方法来关闭连接。
>>> p = r.pubsub()
>>> ...
>>> p.close()

reds pub/sub官方文档翻译的更多相关文章

  1. Flume官方文档翻译——Flume 1.7.0 User Guide (unreleased version)中一些知识点

    Flume官方文档翻译--Flume 1.7.0 User Guide (unreleased version)(一) Flume官方文档翻译--Flume 1.7.0 User Guide (unr ...

  2. Flume官方文档翻译——Flume 1.7.0 User Guide (unreleased version)(二)

    Flume官方文档翻译--Flume 1.7.0 User Guide (unreleased version)(一) Logging raw data(记录原始数据) Logging the raw ...

  3. 蓝牙4.0——Android BLE开发官方文档翻译

    ble4.0开发整理资料_百度文库 http://wenku.baidu.com/link?url=ZYix8_obOT37JUQyFv-t9Y0Sv7SPCIfmc5QwjW-aifxA8WJ4iW ...

  4. GreenDao官方文档翻译(上)

    笔记摘要: 上一篇博客简单介绍了SQLite和GreenDao的比较,后来说要详细介绍下GreenDao的使用,这里就贴出本人自己根据官网的文档进行翻译的文章,这里将所有的文档分成上下两部分翻译,只为 ...

  5. Aircrack-ng官方文档翻译[中英对照]---Airdecap-ng

    Aircrack-ng官方文档翻译---Airdecap-ng   Description[简介] With airdecap-ng you can decrypt WEP/WPA/WPA2 capt ...

  6. Aircrack-ng官方文档翻译[中英对照]---Airmon-ng

    Aircrack-ng官方文档翻译---Airmon-ng Description[简介] This script can be used to enable monitor mode on wire ...

  7. Aircrack-ng官方文档翻译[中英对照]---Aireplay-ng

    Aircrack-ng官方文档翻译---Aireplay-ng[90%] Description[简介] Aireplay-ng is used to inject frames. Aireplay- ...

  8. Salt Stack 官方文档翻译 - 一个想做dba的sa - 博客频道 - CSDN.NET

    OSNIT_百度百科 Salt Stack 官方文档翻译 - 一个想做dba的sa - 博客频道 - CSDN.NET Salt Stack 官方文档翻译 分类: 自动运维 2013-04-02 11 ...

  9. Retrofit官方文档翻译

    Retrofit官方文档翻译 官方文档网址 http://square.github.io/retrofit/ 介绍 Retrofit 将你的 HTTP API 转换为 Java 接口. public ...

随机推荐

  1. Makefile简介

    1.源程序的编译在Linux下面,如果要编译一个C语言源程序,我们要使用GNU的gcc编译器. 下面我们以一个实例来说明如何使用gcc编译器.假设我们有下面一个非常简单的源程序(hello.c):in ...

  2. [51NOD1127]最短的包含字符串(尺取法)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1127 思路:尺取法,一开始我考虑更新右指针,直到遇到一个和l指 ...

  3. React学习、安装及QuickStart

    首先看的是这个页面 http://www.cocoachina.com/webapp/20150721/12692.html 这里有个内容差不多的版本(精华版):http://www.cnblogs. ...

  4. 【温故知新】c#异步编程模型(APM)--使用委托进行异步编程

    当我们用到C#类许多耗时的函数XXX时,总会存在同名的类似BeginXXX,EndXXX这样的函数. 例如Stream抽象类的Read函数就有 public abstract int Read(byt ...

  5. mysql数据库导入外键约束问题

    在网站搬迁过程中,很重要一点是数据的迁移.你的数据库可能已经包含了一个设计良好的数据表集合,并且在网站运营过程中,产生了重要的数据.这时你必须做好包含数据表schema以及数据本身的迁移. 完成上述数 ...

  6. LA 3695 Distant Galaxy

    给出n个点的坐标(坐标均为正数),求最多有多少点能同在一个矩形的边界上. 题解里是构造了这样的几个数组,图中表示的很明白了. 首先枚举两条水平线,然后left[i]表示竖线i左边位于水平线上的点,on ...

  7. HDU 2059 龟兔赛跑

    受上一道题影响,我本来想着开一个二维数组来表示充电和不充电的状态. 可这样就有一个问题,如果没有充电,那么在下一个阶段就有剩余的电量. 这样问题貌似就不可解了,难道是因为不满足动态规划的无后效性这一条 ...

  8. css圣杯布局、等高布局

    所谓圣杯布局,就是自适应浏览器的分辨率,也称双飞翼布局. 例子: <style> body{margin:0;} .center{height:600px;background:#f60; ...

  9. Oracle安装错误ora-00922(zhuan)

    Oracle安装错误ora-00922(缺少或无效选项) (2012-03-19 10:49:27) 转载▼ 标签: 杂谈   安装Oracle 11g R2的过程中,在新建数据库实例时出现了该错误, ...

  10. HDU 5317 RGCDQ (质数筛法,序列)

    题意:从1~1000,000的每个自然数质因子分解,不同因子的个数作为其f 值,比如12=2*2*3,则f(12)=2.将100万个数转成他们的f值后变成新的序列seq.接下来T个例子,每个例子一个询 ...