python的redis,实用订阅发布简单实用代码

服务端:

import redis

rc = redis.Redis(host='127.0.0.1')

ps = rc.pubsub()

ps.subscribe(['foo', 'bar'])  //订阅两个频道,分别是foo,或bar

for item in ps.listen():

    if item['type'] == 'message':

        print item['data']

客户端

import redis

rc = redis.Redis(host='127.0.0.1')

ps = rc.pubsub()

ps.subscribe(['foo', 'bar'])  //订阅两个频道

rc.publish('foo', 'hello world')


打印

hello world

发布了112 篇原创文章 · 获赞 37 · 访问量 68万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章