reds pub/sub官方文档翻译

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()

时间: 2024-10-10 09:36:39

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

Swift 集合类型(Collection Type) 之 字典(dictionary)(官方文档翻译及总结)

Swift语言提供经典的数组和字典两种集合类型来存储集合数据.数组和字典中存储的数据值类型必须明确.这意味着我们不能把不正确的数据类型插入其中.Swift对显式类型集合的使用确保了我们的代码对工作所需要的类型非常清楚.也让我们在开发中可以早早的找到任何的类型不匹配错误.如果你用变量(var)创建的集合,这些集合就是可变的(增删改).如果用常理创建的,这些集合是不能被操作的. 注意: 如果集合的元素是不变的,那就将集合声明为常量.这样Swift编译器能对你创建的集合做性能优化. 1. 数组(arr

NServiceBus官方文档翻译(一)NServiceBus 概况

NServiceBus 概况 NServiceBus 被设计用来组合面向业务的服务,它并不是用来替代诸如 WCF 一类的RPC技术. NServiceBus 不只包含通信模块,像其他成熟的SOA和DDD项目一样,它使用了多种组合的方法和技术. 本篇文章探讨了 NServiceBus 和微软相关产品的相似点和不同点. 相比 BizTalk 更接近 WCF 当人们听到“服务总线”这个名词时,一般会描绘出如上图所示的画面,像 BizTalk 一样所有的通信都经过一个中央结点.这实际上描述的是一个代理的

Python3.2官方文档翻译--标准库概览(一)

7.1 操作系统接口 Os模块提供主要许多与操作系统交互的函数. >>> import os >>> os.getcwd() # Return the current working directory 'C:\\Python31' >>> os.chdir('/server/accesslogs') # Change current working directory >>> os.system('mkdir today') # R

Alljoyn瘦客户端库介绍(官方文档翻译)

Alljoyn瘦客户端库介绍(1) 1.简介 本文档对AllJoynTM瘦客户端的核心库文件(AJTCL)进行了详尽的介绍.本文档介绍了系统整体架构,AllJoyn框架结构,并着重于介绍如何将嵌入式设备加入AllJoyn系统整体架构中.1.1目的 本文档介绍了如何使一个受限于功耗.计算能力和内存的设备(嵌入式设备)加入AllJoyn分布式系统.具体而言,本文档包括了对AllJoyn面向嵌入式系统的方面的介绍,并着重描述了基于AllJoyn的系统的各个组件是如何与嵌入式设备协作以构建一个基于接近式

Oracle 12c 12.1.0.1.0管理控制文件官方文档翻译说明

Link: http://download.csdn.net/detail/rlhua/7718571 官方Link: http://docs.oracle.com/database/121/ADMIN/control.htm#ADMIN006 版本: [email protected]>select * from v$version; BANNER                                                                          

官方文档翻译:Innodb的锁

InnoDB Record, Gap, and Next-Key Locks 考虑到翻译准确性,对于某些特殊名称不做翻译,以免误导: InnoDB has several types of record-level locks including record locks, gap locks, and next-key locks. innodb 有几种行锁类型,包括,record locks(记录锁),gap locks(间隙锁),and next-key locks(组合). Record

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

Aircrack-ng官方文档翻译---Airmon-ng Description[简介] This script can be used to enable monitor mode on wireless interfaces. It may also be used to go back from monitor mode to managed mode. Entering the airmon-ng command without parameters will show the int

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

Aircrack-ng官方文档翻译---Airdecap-ng Description[简介] With airdecap-ng you can decrypt WEP/WPA/WPA2 capture files. As well, it can also be used to strip the wireless headers from an unencrypted wireless capture. It outputs a new file ending with ”-dec.cap”

Swift语言官方文档翻译(2)

A Swift Tour 按照惯例,我们一般学习一个新语言的时候,我们都习惯性的在屏幕上打印"Hello, World",在Swift中,你可以用如下一个单独语句实现 println("Hello,World") 如果你用C或者OC写过程序,那么上面的语句对于你来说是很熟悉的.在Swift中,这一行代码就是一个完整的程序,你不需要为了类似I/O或者String handling去导入一些jar包.全局变量将作为一个程序的入口点,所以你不需要main函数,你也不需要在