发布者:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import redis

class RedisHelper:

    def __init__(self):
        self.__conn = redis.Redis(host='10.211.55.4')
        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()
obj.public('hello world')

 

订阅者:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import redis

class RedisHelper:

    def __init__(self):
        self.__conn = redis.Redis(host='10.211.55.4')
        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)

 

相关文章:

  • 2021-06-08
  • 2021-10-14
  • 2021-12-24
  • 2021-06-30
  • 2021-11-06
猜你喜欢
  • 2021-09-18
  • 2021-11-23
  • 2021-10-15
  • 2021-08-06
  • 2021-12-05
相关资源
相似解决方案