redis pipeset发布订阅

本文介绍了一种利用Redis实现发布订阅机制的方法。通过定义频道并使用自定义类封装发布和订阅功能,实现了消息的发送与接收。示例代码展示了如何设置频道、发布消息及订阅并解析接收到的消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/usr/bin/env python
# Author:Zhangmingda
import redis,time

pool = redis.ConnectionPool(host='192.168.11.5',port=6379,db=2)
r = redis.Redis(connection_pool=pool)
pipe = r.pipeline(transaction=True)
pipe.set('age','22')
time.sleep(15)

pipe.execute()  #如果不执行,则不会执行pipe.set 即不会真正存值
pipeset
#!/usr/bin/env python
# Author:Zhangmingda
#!/usr/bin/env python
# Author:Zhangmingda
import redis

class RedisHelper:
    def __init__(self):
        self.__conn = redis.Redis(host='192.168.11.5',port=6379)
        self.chan_sub = 'fm104.5'
        self.chan_pub = 'fm104.5'

    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

obj = RedisHelper()
redis_sub = obj.subscribe()
while True:
    msg = redis_sub.parse_response()
    print(msg)
redis_publish发布订阅
#!/usr/bin/env python
# Author:Zhangmingda
import redis
class RedisHelper:
    def __init__(self):
        self.__conn = redis.Redis(host='192.168.11.5',port=6379)
        self.chan_sub = 'fm104.5'
        self.chan_pub = 'fm104.5'

    def public(self, msg): #发布消息用函数
        self.__conn.publish(self.chan_pub, msg)
        return True

server = RedisHelper()
server.public('hehe')
redis发布消息

 http://www.cnblogs.com/lianzhilei/p/5983673.html

转载于:https://www.cnblogs.com/zhangmingda/p/9498537.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值