RedisHelper

import redis


class RedisHelper:
    def __init__(self):
        self.__conn = redis.Redis(host='47.94.18.xxx')
        self.chan_sub = '104.5'  # 接收频道
        self.chan_pub = '104.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

订阅者:

from redis_test.rds_helper import RedisHelper

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

while True:
    msg = redis_sub.parse_response()
    print(msg)

发布者:

from redis_test.rds_helper import RedisHelper

obj = RedisHelper()
obj.public('hello')

 

相关文章:

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