1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| http://redis.io/topics/pubsub
RedisHelper.py #!/usr/bin/env python # coding: utf-8 __author__ = 'whoami'
""" @version: 1.0 @author: whoami @license: Apache Licence 2.0 @contact: [email protected] @site: http://www.itweet.cn @software: PyCharm Community Edition @file: RedisHelper.py @time: 2015-11-25 下午11:11 """ import redis
class RedisHelper:
def __init__(self): self.__conn = redis.Redis(host='127.0.0.1') self.chan_sub = 'fm87.7' self.chan_pub = 'fm87.7'
def get(self,key): return self.__conn.get(key)
def set(self,key,value): return self.__conn.set(key,value)
def public(self,msg): self.__conn.publish(self.chan_pub,msg) return True
def subscribe(self): pub = self.__conn.pubsub() pub.subscribe(self.chan_sub) pub.parse_response() return pub
if __name__ == '__main__': t = RedisHelper() t.public('test')
#测试订阅发布功能 whoami@ubuntu:~/py/demo/day8$ python RedisHelper.py
#此时收不到消息,因为是实时的,只有开启此频道,在发送才能接受到消息 whoami@ubuntu:~/py/demo/day8$ python Python 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import tab,RedisHelper >>> r = RedisHelper.RedisHelper() >>> recv = r.subscribe() >>> recv.parse_response() #这一步的时候在发送就会不断收到客户端发送过来的消息 >>> recv.parse_response() #阻塞模式,没有数据就阻塞,有数据就返回 ['message', 'fm87.7', 'test'] >>> recv.parse_response() ['message', 'fm87.7', 'test1'] >>> recv.parse_response() ['message', 'fm87.7', 'test2']
>>> recv.get_message() #非阻塞,如果没有消息立即返回,有消息立即处理 {'pattern': None, 'type': 'message', 'channel': 'fm87.7', 'data': 'whoami'}
=========================================================================== ++++++++++++++++https://pypi.python.org/pypi/redis/2.10.5++++++++++++++++++ Publish / Subscribe redis-py includes a PubSub object that subscribes to channels and listens for new messages. Creating a PubSub object is easy.
>>> r = redis.StrictRedis(...) >>> p = r.pubsub() Once a PubSub instance is created, channels and patterns can be subscribed to.
>>> 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.
>>> 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.
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. 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. ===========================================================================
|