python使用redis模塊來(lái)跟redis實(shí)現(xiàn)交互
redis模塊的使用
1.安裝模塊
pip3 install redis
2.導(dǎo)入模塊
import redis
3.連接方式
- 嚴(yán)格連接模式:r=redis.StrictRedis(host=“”,port=)
- 更Python化的連接模式:r=redis.Redis(host=“”,port=)
- StrictRedis用于實(shí)現(xiàn)大部分官方的命令,并使用官方的語(yǔ)法和命令
- Redis與StrictRedis的區(qū)別是:Redis是StrictRedis的子類,用于向前兼容舊版本的redis-py,并且這個(gè)連接方式是更加"python化"的
4.連接池
為了節(jié)省資源,減少多次連接損耗,連接池的作用相當(dāng)于總攬多個(gè)客戶端與服務(wù)端的連接,當(dāng)新客戶端需要連接時(shí),只需要到連接池獲取一個(gè)連接即可,實(shí)際上只是一個(gè)連接共享給多個(gè)客戶端。
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())#可以看出兩個(gè)連接的id是一致的,說(shuō)明是一個(gè)客戶端連接5.操作
- 值的設(shè)置和獲取,可以參考redis的命令,redis模塊中的對(duì)應(yīng)功能的函數(shù)名基本與redis中的一致
- 注意默認(rèn)情況下,設(shè)置的值或取得的值都為bytes類型,如果想改為str類型,需要在連接時(shí)添加上decode_responses=True】
- 設(shè)置值:
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() #其他。。?;緍edis的命令名與redis模塊中的函數(shù)名一致
獲取:
redis中g(shù)et() ==》r.get() redis中mget() ==》r.mget() redis中g(shù)etset() ==》r.getset() redis中g(shù)etrange() ==》r.getrange() #其他。。?;緍edis的命令名與redis模塊中的函數(shù)名一致
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'))管道
一般情況下,執(zhí)行一條命令后必須等待結(jié)果才能輸入下一次命令,管道用于在一次請(qǐng)求中執(zhí)行多個(gè)命令。
參數(shù)介紹:
transaction:指示是否所有的命令應(yīng)該以原子方式執(zhí)行。
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()事務(wù)
python中可以使用管道來(lái)代替事務(wù):
補(bǔ)充:監(jiān)視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")訂閱\發(fā)布
發(fā)布方:
import redis
r=redis.Redis(host="localhost",port=6379,decode_responses=True)
#發(fā)布使用publish(self, channel, message):Publish ``message`` on ``channel``.
Flag=True
while Flag:
msg=input("主播請(qǐng)講話>>:")
if len(msg)==0:
continue
elif msg=='quit':
break
else:
r.publish('cctv0',msg)訂閱方:
當(dāng)訂閱成功后,第一次接收返回的第一個(gè)消息是一個(gè)訂閱確認(rèn)消息:

import redis
r=redis.Redis(host="localhost",port=6379,decode_responses=True)
#發(fā)布使用publish(self, channel, message):Publish ``message`` on ``channel``.
Flag=True
chan=r.pubsub()#返回一個(gè)發(fā)布/訂閱對(duì)象
msg_reciver=chan.subscribe('cctv0')#訂閱
msg=chan.parse_response()#第一次會(huì)返回訂閱確認(rèn)信息
print(msg)
print("訂閱成功,開(kāi)始接收------")
while Flag:
msg=chan.parse_response()#接收消息
print(">>:",msg[2])#此處的信息格式['消息類型', '頻道', '消息'],所以使用[2]來(lái)獲取到此這篇關(guān)于python使用redis模塊來(lái)跟redis實(shí)現(xiàn)交互的文章就介紹到這了,更多相關(guān)python redis交互內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python?Opencv實(shí)現(xiàn)圖片切割處理
這篇文章主要為大家詳細(xì)介紹了Python?Opencv實(shí)現(xiàn)圖片切割處理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
windows10在visual studio2019下配置使用openCV4.3.0
這篇文章主要介紹了windows10在visual studio2019下配置使用openCV4.3.0,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Python真題案例之錯(cuò)位鍵盤(pán)?單詞長(zhǎng)度?字母重排詳解
這篇文章主要介紹了python實(shí)操案例練習(xí),本文給大家分享的案例中主要任務(wù)有錯(cuò)位鍵盤(pán)、單詞長(zhǎng)度、字母重排,需要的小伙伴可以參考一下2022-03-03
Django項(xiàng)目uwsgi+Nginx保姆級(jí)部署教程實(shí)現(xiàn)
這篇文章主要介紹了Django項(xiàng)目uwsgi+Nginx保姆級(jí)部署教程實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
使用PyInstaller庫(kù)把Python程序打包成exe
這篇文章介紹了使用PyInstaller庫(kù)把Python程序打包成exe的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
用十張圖詳解TensorFlow數(shù)據(jù)讀取機(jī)制(附代碼)
這篇文章主要介紹了用十張圖詳解TensorFlow數(shù)據(jù)讀取機(jī)制(附代碼),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
Python使用tablib生成excel文件的簡(jiǎn)單實(shí)現(xiàn)方法
這篇文章主要介紹了Python使用tablib生成excel文件的方法,結(jié)合實(shí)例形式分析了tablib模塊的相關(guān)使用技巧,需要的朋友可以參考下2016-03-03
使用SAE部署Python運(yùn)行環(huán)境的教程
這篇文章主要介紹了使用SAE部署Python運(yùn)行環(huán)境的教程,SAE作為新浪的在線軟件部署平臺(tái),在國(guó)內(nèi)擁有一定的性價(jià)比,需要的朋友可以參考下2015-05-05

