Python for Redis subscriptions and publishing

Source: Internet
Author: User

subscribers can subscribe to one or more channels, and when a publisher sends a message to a channel, all subscribers who subscribe to the channel will receive a message, and the Publisher will receive a numeric value that is the number of subscribers who received the message. A subscriber can only receive a message from the publisher after it has started subscribing, and it is impossible to receive a previously published message.

Many do not say look at the code!!!

Process-oriented approach

#!/usr/bin/python

#coding: Utf-8


#服务器端

Import Redis

R = Redis. Redis (host= ' 127.0.0.1 ', port= ' 6379 ')#连接redis

p = r.pubsub () #开启订阅

P.subscribe (' 6379 ') #接收订阅的数据, subscribed channels


For item in P.listen (): #读取接收的数据

Print Item

If item[' type '] = = ' message ': #判断数据是否是用户发布的数据

data = item[' data ' #取出用户要发布的数据

Print Data #打印要发布的数据


If item[' data ' = = ' Q ' or item[' data ' = = ' Q ':

Break #退出程序

P.unsubscribe (' 6379 ')#关闭频道

print ' unsubscribe '


#客户端

#!/usr/bin/py

#coding: Utf-8

Import Redis

R = Redis. Redis (host= ' 127.0.0.1 ', port=6379)#连接redis

While True:

My_input = raw_input ("Please enter content:")#输入发布的内容

R.publish (' 6379 ', my_input)#发送到的频道, published content


if my_input = = ' Q ' or my_input = = ' Q ': #判断用户是否要退出程序

print ' Stop publishing '

Break


Object-oriented approach

#服务器端

#!/usr/bin/python

#coding: Utf-8

Import Redis


Class Server (object):

def __init__ (self,ip= ' 127.0.0.1 ', port=6379,sub= ' A '):

Self.ip = IP

Self.port = Port

Self.connect = Redis. Redis (host=self.ip,port=self.port) #连接redis

Self.sub = Sub #监听频道

Def SE (self):

Spub = Self.connect.pubsub ()#打开订阅

Spub.subscribe (self.sub)#开始监听

Spub.listen ()#用户发布的数据

Return spub


x = Server ()

p = x.se ()

For item in P.listen (): #打印接收到的数据

Print Item


#客户端

#!/usr/bin/python

#coding: Utf-8

Import Redis


Class Client (object):

def __init__ (self,ip= ' 127.0.0.1 ', port=6379,pub= ' A '):

Self.ip = IP

Self.port = Port

Self.connect = Redis. Redis (Host=self.ip,port=self.port)

Self.pub = Pub #连接的频道

DEF cl (self,content):

Self.connect.publish (self.pub,content)#频道, the data sent


x = Client ()

While True:

My_input = raw_input (' Please enter content: ') #发布的数据

x.cl (My_input)


Personal comparison like the second method, also recommend friends to use the second method, the second method I did not connect to receive the data to do too much processing, if you friends like can be taken to modify their own

This article is from the "Automated Operations" blog, please be sure to keep this source http://hongchen99.blog.51cto.com/12534281/1909790

Python for Redis subscriptions and publishing

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.