python redis模块订阅与发布

来源:互联网 发布:linux网络监控软件 编辑:程序博客网 时间:2024/03/28 21:46

订阅:

import redisdef redis_sub(*cha):"""redis客户端订阅多个信道"""conn = redis.StrictRedis()ps = conn.pubsub()#订阅信道ps.subscribe(*cha)#监听发布的信息for info in ps.listen():if info['type'] == 'message':msg = info['data']if info['channel'] == 'cha_first':print 'the cha_first message:',msgelif info['channel'] == 'cha_sec':print 'the cha_sec message:',msg

发布:

import redisdef redis_pub(cha,msg):"""redis发布信息"""conn = redis.StrictRedis()#发布信息conn.publish(cha,msg)


0 0