缓存数据库-redis(订阅发布)
时间:2021-07-01 10:21:17
帮助过:13人阅读
-*- coding:utf-8 -*-
__author__ =
‘shisanjun‘
import redis
class RedisHelper(object):
def __init__(self):
self.__conn=redis.Redis(host=
"192.168.0.121")
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
订阅
# -*- coding:utf-8 -*-
__author__ = ‘shisanjun‘
from redishelp import RedisHelper
obj=RedisHelper()
redis_sub=obj.subscribe()
while True:
msg=redis_sub.parse_response()
print(msg)
发布
# -*- coding:utf-8 -*-
__author__ = ‘shisanjun‘
from redishelp import RedisHelper
obj=RedisHelper()
obj.public("hello")
缓存数据库-redis(订阅发布)
标签:scribe 消息 打开 src init ima imp img run