Python Redis Publishing and subscription

Source: Internet
Author: User



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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.