Python's subscription publishing feature for Redis operations

Source: Internet
Author: User
Tags install redis

Installing Redis-server

Yum-y Install gcc gcc-c++ #安装编译工具

Cd/opt

Wget-c http://download.redis.io/releases/redis-3.0.5.tar.gz #下载包

Tar XF redis-3.0.5.tar.gz #解压

CD redis-3.0.5

Make MALLOC=LIBC #编译

Make Prefix=/usr/local/redis Install #安装

Echo ' Path=/usr/local/redis/bin: $PATH ' >>/etc/profile #配置环境变量

Source/etc/profile #或. /etc/profile

Mkdir/usr/local/redis/conf #创建目录

cp/opt/redis-3.0.5/redis.conf/usr/local/redis/conf/#拷贝配置文件

Sed-i "46s#/var/run/redis.pid#/usr/local/redis/conf/redis.pid#"/usr/local/redis/conf/redis.conf

Sed-i "192s#./#/usr/local/redis/conf/#"/usr/local/redis/conf/redis.conf

Sed-i 509s/no/yes//usr/local/redis/conf/redis.conf

Nohup redis-server/usr/local/redis/conf/redis.conf &>/dev/null &

echo vm.overcommit_memory = 1 >>/etc/sysctl.conf

Sysctl-p

lsof-i:6379 #查看端口有没有起来


Install the PIP command Dependency package

wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz--no-check-certificate

CD SETUPTOOLS-0.6C11

Python setup.py Build

Python setup.py Install


Install the PIP command

wget "HTTPS://PYPI.PYTHON.ORG/PACKAGES/SOURCE/P/PIP/PIP-1.5.4.TAR.GZ#MD5=834B2904F92D46AAA333267FB1C922BB"-- No-check-certificate

Tar XF pip-1.5.4.tar.gz

CD pip-1.5.4

Python setup.py Install


Installing the Python API for Redis

Pip Install Redis


Test

[[email protected] opt]# python

Python 2.6.6 (r266:84292, Nov 22 2013, 12:16:22)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2

Type "Help", "copyright", "credits" or "license" for more information.

>>> Import Redis

>>> r = Redis. Redis (host= ' 10.211.55.4 ', port=6379)

>>> r.set (' foo ', ' Bar ')

>>> print r.get (' foo ')

"Bar"


[Email protected] opt]# REDIS-CLI

127.0.0.1:6379> get foo

"Bar"



Redis Subscription and publishing features demo

[email protected] opt]# cat redishelper.py

#!/usr/bin/env python

#coding: Utf-8


Import Redis


Class Redishelper:


def __init__ (self):

Self.__conn = Redis. Redis (host= ' 127.0.0.1 ')

self.chan_sub = ' fm87.7 ' #订阅频道

self.chan_pub = ' fm87.7 ' #接收频道


def get (Self,key):

return Self.__conn.get (Key)


def set (Self,key,value):

Self.__conn.set (Key,value)


def public (self,msg): #在chan_pub这个频道发布消息

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 #开始接收


if __name__ = = ' __main__ ':

t = Redishelper ()

T.public (' Test ') #发布了test这个消息



Execute script at one end

Python redishelper.py


View at the other end

>>> Import Redishelper

>>> r = Redishelper. Redishelper ()

>>> R.subscribe ()

<redis.client.pubsub Object at 0x7f1789422950>

>>> recv = R.subscribe ()

>>> Recv.parse_response ()

[' Message ', ' fm87.7 ', ' Test ']



Custom-made reading demo 2

#在一个终端打开这个

>>> Import Redis

>>> r = Redis. Redis (host= ' 127.0.0.1 ')

>>> chan = r.pubsub () #打开频道

>>> chan.subscribe ("fm100") #调到该频道

>>> Chan.parse_response () #测试监听频道

[' subscribe ', ' fm100 ', 1L]

>>> Chan.parse_response () #再次执行成阻塞状态等待消息的到来


>>> while True: #写成死循环

... chan.parse_response ()



Open a publish message at another terminal

>>> Import Redis

>>> p = Redis. Redis (host= ' 127.0.0.1 ')

>>> p.publish (' fm100 ', ' hellow world!!! ')

1L


Back before that terminal found already out message

>>> while True:

... chan.parse_response ()

...

[' Message ', ' fm100 ', ' hellow world!!! ']


This article is from the "Wsyht90 blog" blog, make sure to keep this source http://wsyht90.blog.51cto.com/9014030/1845888

Python's subscription publishing feature for Redis operations

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.