python之redis

来源:互联网 发布:rnaseq数据分析流程 编辑:程序博客网 时间:2024/05/29 17:11

一. 安装与启动

1下载安装包下载地址:http://download.redis.io/releases/下载完成后上传服务器 解压、编译 、安装。。。

安装过程比较简单,带过..

启动:

输入命令 redis-server

2.连接

#!/usr/bin/env python# -*- coding:utf-8 -*-import redisr = redis.Redis(host='192.168.0.1', port=6379,db=0)r.set('name', 'zhangsan')   #添加print (r.get('name'))   #获取
3. 连接池

#!/usr/bin/env python# -*- coding:utf-8 -*-import redispool = redis.ConnectionPool(host='192.168.0.1', port=6379)r = redis.Redis(connection_pool=pool)r.set('userid', 'maple')   #添加print r.get('name')   #获取

4.发布和订阅

#!/usr/bin/env python#-*- coding:utf-8 -*-import redisclass RedisHelper(object):    def __init__(self):        self.__conn = redis.Redis(host='192.168.0.1',port=6379)#连接Redis        self.channel = 'hello'    def publish(self,msg):#发布        self.__conn.publish(self.channel,msg)        return True    def subscribe(self):#定义订阅方法        pub = self.__conn.pubsub()        pub.subscribe(self.channel)        pub.parse_response()        return pub

原创粉丝点击