1. 本际云推荐 - 专业推荐VPS、服务器,IDC点评首页
  2. 云主机运维
  3. VPS运维

python使用redis控制模块来和redis完成互动

Python中使用Redis控制模块和Redis互动的详细介绍

我是本际云服务器推荐网的小编小本本,今天要带大家来详细介绍一下Python使用Redis控制模块和Redis互动的基本操作,本文内容紧扣主题,具有很强的实用价值,需要的朋友可以认真学习哦。

python使用redis控制模块来和redis完成互动

Redis模块的使用

1. 安装模块:pip3 install redis
2. 导入模块:import redis
3. 接口方式:
严苛连接模式:r=redis.StrictRedis(host=””,port=)
更Python变的连接模式:r=redis.Redis(host=””,port=)
StrictRedis用以完成绝大多数官方指令,并用官方词汇和指令。Redis与StrictRedis的区别在于,Redis是StrictRedis的派生类,用以往前适配旧版的redis-py,而且这个接口方式是更”python化”的。
4. 连接池:
为了节约网络资源,降低数次联接消耗,连接池的功效等同于总览好几个手机客户端与服务器端连接,当新手机客户端需用联接时,只必须到连接池获得1个联接就可以,事实上就是一个连接共享给好几个手机客户端。

import redis
pool=redis.ConnectionPool(host=’localhost’,port=6379,decode_responses=True)
r=redis.Redis(connection_pool=pool)
r2=redis.Redis(connection_pool=pool)
r.set(‘apple’,’a’)
print(r.get(‘apple’))
r2.set(‘banana’,’b’)
print(r.get(‘banana’))
print(r.client_list())
print(r2.client_list())
可以看出两个连接的id是一致的,说明是一个客户端连接。
5. 实际操作:
系数的设定和获得,可以参考一下Redis的指令,Redis控制模块里的相匹配功能性的函数名基本和Redis中的认可。需要注意,默认设置前提下,设定的值或获得数值均为bytes类型,如果要改成str类型,必须在连接时加上decode_responses=True。

设定值:
Redis中set()==>r.set()
Redis中setnx()==>r.set()
Redis中setex()==>r.setex()
Redis中setbit()==>r.setbit()
Redis中mset()==>r.mset()
Redis中hset()==>r.hset()
Redis中sadd()==>r.sadd()
#其他。。。基本Redis的命令名与Redis模块中的函数名一致
获取:
Redis中get()==>r.get()
Redis中mget()==>r.mget()
Redis中getset()==>r.getset()
Redis中getrange()==>r.getrange()
#其他。。。基本Redis的命令名与Redis模块中的函数名一致

import redis
r=redis.Redis(host=’localhost’,port=6379,decode_responses=True)
#r=redis.StrictRedis(host=’localhost’,port=6379)
r.set(‘key’,’value’)
value=r.get(‘key’)
#print(type(value))
print(value)
r.hset(‘info’,’name’,’lilei’)
r.hset(‘info’,’age’,’18’)
print(r.hgetall(‘info’))
r.sadd(‘course’,’math’,’english’,’chinese’)
print(r.smembers(‘course’))

管道

一般情况下,执行一条命令后必须等待结果才能输入下一次命令,管道用于在一次请求中执行多个命令。
参数介绍:transaction:指示是否所有的命令应该以原子方式执行。

import redis
import time
r=redis.Redis(host=”localhost”,port=6379,decode_responses=True)
pipe=r.pipeline(transaction=True)
pipe.set(‘p1′,’v2’)
pipe.set(‘p2′,’v3’)
pipe.set(‘p3′,’v4’)
time.sleep(5)
pipe.execute()

事务

Python中可以使用管道来代替事务。
补充:监视watch:pipe.watch()

import redis
import time
import redis.exceptions
r=redis.Redis(host=’localhost’,port=6379,decode_responses=True)
pipe=r.pipeline()
print(r.get(‘a’))
try:
#pipe.watch(‘a’)
pipe.multi()
pipe.set(‘here’,’there’)
pipe.set(‘here1′,’there1’)
pipe.set(‘here2′,’there2’)
time.sleep(5)
pipe.execute()
except redis.exceptions.WatchError as e:
print(“Error”)

订阅发布

发布方:

import redis
r=redis.Redis(host=”localhost”,port=6379,decode_responses=True)
#发布使用publish(self,channel,message):Publish"message"on"channel".
Flag=True
while Flag:
msg=input(“主播请讲话>>:”)
if len(msg)==0:
continue
elif msg==’quit’:
break
else:
r.publish(‘cctv0’,msg)

订阅方:
当订阅成功后,第一次接收返回的第一个消息是一个订阅确认消息。

import redis
r=redis.Redis(host=”localhost”,port=6379,decode_responses=True)
#发布使用publish(self,channel,message):Publish"message"on"channel".
Flag=True
chan=r.pubsub()#返回一个发布/订阅对象
msg_reciver=chan.subscribe(‘cctv0’)#订阅
msg=chan.parse_response()#第一次会返回订阅确认信息
print(msg)
print(“订阅成功,开始接收——“)
while Flag:
msg=chan.parse_response()#接收消息
print(“>>:”,msg[2])#此处的信息格式['消息类型','频道','消息'],所以使用[2]来获取

以上就是Python使用Redis控制模块和Redis互动的详细介绍,希望对大家有所帮助哦!

原创文章,作者:小编小本本,如若转载,请注明出处:https://www.benjiyun.com/yunzhujiyunwei/vps-yunwei/6906.html