安装: pip install redis 发布文件:# coding:utf-8import redisimport jsonclass RedisHelper():def __init__(self):localhost = 192.168.2.xself.__conn = redis.Redis(host=localhost, port=6379, password=pa...
安装:
pip install redis
发布文件:
# coding:utf-8 import redis import json class RedisHelper(): def __init__(self): localhost = '192.168.2.x' self.__conn = redis.Redis(host='localhost', port=6379, password='password') # , decode_responses=True, password='password' self.channel = "monitor" def publisher(self, msg): self.__conn.publish(self.channel, json.dumps(msg)) def subscriber(self): pub = self.__conn.pubsub() pub.subscribe(self.channel) pub.parse_response() return pub if __name__ == "__main__": msg = { 'name': 'publisher', 'msg': 'message' } red = RedisHelper() red.publisher(msg) print msg
订阅文件:
# coding:utf-8 from redis_practice import * obi = RedisHelper() redis_sub = obi.subscriber() msg = redis_sub.parse_response() print msg
沃梦达教程
本文标题为:python:用Redis完成发布和订阅数据
猜你喜欢
- 搭建单机Redis缓存服务的实现 2023-07-13
- redis清除数据 2023-09-13
- Oracle 删除大量表记录操作分析总结 2023-07-23
- 基于Python制作一个简单的文章搜索工具 2023-07-28
- 在阿里云CentOS 6.8上安装Redis 2023-09-12
- SQL Server 2022 AlwaysOn新特性之包含可用性组详解 2023-07-29
- Numpy中如何创建矩阵并等间隔抽取数据 2023-07-28
- SQLSERVER调用C#的代码实现 2023-07-29
- Mongodb启动报错完美解决方案:about to fork child process,waiting until server is ready for connections. 2023-07-16
- MySQL8.0.28安装教程详细图解(windows 64位) 2023-07-26