怪物奇妙物语

宇宙无敌超级美少男的怪物奇妙物语

首页 新随笔 联系 管理
  819 随笔 :: 0 文章 :: 2 评论 :: 16万 阅读

/Users/song/Code/redis_learn/le00.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import asyncio
from redis import Redis,AuthenticationError,TimeoutError
REDIS_OPEN: bool = False
REDIS_HOST: str = '127.0.0.1'
REDIS_PORT: int = 6379
REDIS_PASSWORD: str = ''
REDIS_DATABASE: int = 0
REDIS_TIMEOUT: int = 5
class RedisCli(Redis):
def __init__(self):
super(RedisCli, self).__init__(
host=REDIS_HOST,
port=REDIS_PORT,
password=REDIS_PASSWORD,
db=REDIS_DATABASE,
socket_timeout=REDIS_TIMEOUT,
decode_responses=True # 转码 utf-8
)
async def init_redis_connect(self):
"""
触发初始化连接
:return:
"""
try:
self.ping()
except TimeoutError:
print("连接redis超时")
sys.exit()
except AuthenticationError:
print("连接redis认证失败")
sys.exit()
except Exception as e:
print('连接redis异常 {}', e)
sys.exit()
async def conn_redis(redis_client):
await redis_client.init_redis_connect()
if __name__=="__main__":
# 创建redis连接对象
redis_client = RedisCli()
asyncio.run(conn_redis(redis_client))
redis_client.set('name','alice')
res= redis_client.get('name')
print(res)
# 关闭redis连接
redis_client.close()
```
# requirements.txt
async-timeout==4.0.2
redis==4.5.1
posted on   超级无敌美少男战士  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验
历史上的今天:
2022-02-12 css 四种基本选择器
2022-02-12 四种基本选择器
2022-02-12 css的三种引入方式
2022-02-12 html中元素之间的关系
2022-02-12 块级元素 行内元素
2022-02-12 代码提示 thisArg argArray
2022-02-12 如何监听一个对象
点击右上角即可分享
微信分享提示