help 类:

import redis

class RedisHelper:
    def __init__(self):
        self.__conn = redis.Redis(host='192.168.117.138')
        self.chan_sub = 'test'
        self.chan_pub= 'test'

#发送消息
    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

 

订阅者:

'''订阅方'''

from  redishelper import RedisHelper

obj = RedisHelper()
redis_sub = obj.subscribe()

while True:
    msg = redis_sub.parse_response()
    print('接收:',msg)

 

 

发布者:

'''
发布者
'''

from  redishelper import RedisHelper

obj = RedisHelper()
obj.public('how are you?')

 

相关文章:

  • 2022-01-19
  • 2022-12-23
  • 2021-11-30
  • 2021-11-17
  • 2021-12-25
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-02-10
  • 2021-05-19
  • 2021-05-31
  • 2021-05-24
  • 2022-12-23
相关资源
相似解决方案