網站首頁 編程語言 正文
模塊(redis)
Ubuntu
sudo pip3 install redis
使用流程
import redis
# 創建數據庫連接對象
r = redis.Redis(host='127.0.0.1',port=6379,db=0,password='123456')
代碼示例
import redis
# 1 創建redis數據庫連接對象
r = redis.Redis(password='123456')
# 2 使用,很多命令的返回值是字節串,需要用字符串
# ?表示時,調用decode方法.
# 3.1 通用命令...
print(r.keys('*'))
print(r.exists('name'))
# 3.2 字符串類型的操作
r.set('uname', 'aid2102', 60)
print(r.get('uname').decode())
r.mset({'a': 100, 'b': 200, 'c': 300})
print(r.mget(['a', 'b', 'c']))
# 3.3 列表類型的操作
r.lpush('pylk1',100,200,300)
print(r.lrange('pylk1',0,-1))
list案例: 一個進程負責生產任務,一個進程負責消費任務
進程1: 生產者,product.py
import redis
# 1 創建redis數據庫連接對象
r = redis.Redis(password='123456')
# 2.向任務隊列中添加任務,使用列表類型
# 任務格式: 任務類別_發送者_接收者_內容
task = 'sendMail_aid2102_mzg_helloworld'
r.lpush('lst:tasks',task)
進程2: 消費者,consumer.py
import redis
r = redis.Redis(password='123456')
while True:
? ? task = r.brpop('lst:tasks',3)
? ? if task:
? ? ? ? print(task)
? ? ? ? # (b'lst:tasks', b'sendMail_aid2102_mzg_helloworld')
? ? ? ? # 處理任務
? ? ? ? task_data = task[1].decode()
? ? ? ? print(task_data)
? ? ? ? # sendMail_aid2102_mzg_helloworld
? ? ? ? lst_data = task_data.split('_')
? ? ? ? if lst_data[0] == 'sendMail':
? ? ? ? ? ? print('執行發送郵件的任務...')
? ? else:
? ? ? ? print('-no task!-')
原文鏈接:https://blog.csdn.net/weixin_45185267/article/details/125295005
相關推薦
- 2023-05-09 React組件三大核心屬性State?props?Refs介紹_React
- 2022-05-06 mac goland 常用快捷鍵
- 2022-11-17 Android對話框AlertDialog與DatePickerDialog及TimePickerD
- 2022-04-09 解決使用docker權限不足需要加sudo問題,Got permission denied whil
- 2022-11-17 Rust中的Cargo構建、運行、調試_相關技巧
- 2023-10-14 C/C++ 批量梯度下降法實現一元線性回歸
- 2022-07-28 Jquery回調對象與延遲對象用法詳解_jquery
- 2023-05-20 Kotlin作用域函數使用示例詳細介紹_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支