码迷,mamicode.com
首页 > 其他好文 > 详细

发布和订阅

时间:2019-01-20 18:02:10      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:sub   ret   nbsp   scribe   col   1.5   def   span   print   

 

发布者:

#!/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)

 

发布和订阅

标签:sub   ret   nbsp   scribe   col   1.5   def   span   print   

原文地址:https://www.cnblogs.com/sea-stream/p/10295566.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!