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

你可能感兴趣的:(redis,python,订阅,发布,Pub)