Python中redis如何使用,怎么实现交互
Admin 2022-08-04 群英技术资讯 1046 次浏览
今天小编跟大家讲解下有关“Python中redis如何使用,怎么实现交互”的内容 ,相信小伙伴们对这个话题应该有所关注吧,小编也收集到了相关资料,希望小伙伴们看了有所帮助。pip3 install redis
import redis
3.连接方式
为了节省资源,减少多次连接损耗,连接池的作用相当于总揽多个客户端与服务端的连接,当新客户端需要连接时,只需要到连接池获取一个连接即可,实际上只是一个连接共享给多个客户端。
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是一致的,说明是一个客户端连接
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,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,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]来获取
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
在开始学习前,需要准备好相应的环境配置。这里我选择了anaconda,创建了一个专门的虚拟环境来学习机器学习。这里关于anaconda的安装等就不赘述了,没有难度。
这篇文章主要介绍了python性能检测工具函数运行内存及运行时间,python虽然是一门慢语言,但是也有着比较多的性能检测工具来帮助我们优化程序的运行效率,下文小编给大家分享五个性能检测工具,需要的朋友可以参考一下
这篇文章主要介绍了Python 实现多表和工作簿合并及一表按列拆分,文章围绕主题展开详细的资料介绍,具有一定的参考价值,需要的小伙伴可以参考一下
这篇文章主要介绍了Python中的列表条件求和方法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
使用Hadoop进行大数据运算,当数据量极其大时,那么对MapReduce性能的调优重要性不言而喻,尤其是Shuffle过程中的参数配置对作业的总执行时间影响特别大。下面总结一些和MapReduce相关的性能调优方法,主要从五个方面考虑:数据输入、Map阶段、Reduce阶段、Shuffle阶段和其他调优属性。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008