异步编程用官方模块asyncio实现

注意导入的库是redis.asyncio。需要在连接、设置、获取等使用redis的地方可等待。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import asyncio 

async def async_singal():

from redis.asyncio import StrictRedis

ip = "172.17.0.8"
redis_conn = await StrictRedis(
host=ip,
port=6379,
encoding="utf8",
decode_responses=True,
db=0,
)

await redis_conn.set("name", "async singal")
res = await redis_conn.get("name")
print(res)


asyncio.run(async_singal())

结果:

1
2
(ymir) ➜  Desktop python redis_learn.py
async singal