Publish and subscribe
Similar to RSS
Publisher: Server
Subscribers: Dashboad and data processing
Look at the following code:
class file name: monitor.py:
#!/usr/bin/python# -*- coding: utf-8 -*-__author__ = ' Gaogd ' import Redisclass redishelper: def __init__ (self): self.__conn = redis. Redis (host= ' 192.168.10.12 ', port=6379, password= ' wdzj2014 ') self.chan_sub = ' fm250 ' self.chan_pub = ' fm250 ' def public (self, msg): ## Send message to fm250 this channel self.__conn.publish ( SELF.CHAN_PUB, MSG) return True def subscribe (self): # #订阅 (Receive FM250 message on this channel) pub=self.__conn.pubsub () pub.subscribe (self.chan_sub) pub.parse_response () return pub
Subscriber file name: subscriber.py
#!/usr/bin/python#-*-coding:utf-8-*-__author__ = ' gaogd ' ' subscribe ' to ' Import Sys,ossys.path.append ' (Os.path.dirname (__ file__)) from redis_monitor import redishelperobj = Redishelper () redis_sub = Obj.subscribe () # #订阅while true:msg = r Edis_sub.parse_response () print (msg)
Publisher file name: publisher.py
#!/usr/bin/python# -*- coding: utf-8 -*-__author__ = ' gaogd ' release ' Import sys,ossys.path.append (Os.path.dirname (__file__)) From redis_ Monitor import redishelperobj = redishelper () while true: Content = raw_input (U ' Please enter what you want to post: '). Strip () if content == ' Exit ': break obj.public (content) ## Send content
It is important to note that The result of the output is a list, where the elements are of byte type !
This article from the "Struggle Bar" blog, reproduced please contact the author!
Python Redis Publishing and subscription