python-redis-訂閱和發布


發布:redishelper.py
import redis

class RedisHelper:

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

 

訂閱:redis_pub.py

from redishelper import RedisHelper

obi = RedisHelper()#實例化

redis_sub = obi.subscribe()


while True:
    msg =redis_sub.parse_response()
    print(msg)#收到就打印收不到就卡住

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM