Python async redis 给出错误 AttributeError : __aexit__

标签 python python-3.x redis async-await sanic

我是 Python 的新手,正在尝试构建一个简单的应用程序来从 Redis 数据库中检索数据。但是,当我尝试检索数据时,出现“AttributeError: __aexit_”错误。

有人知道我做错了什么吗?提前致谢!

我的代码:

from sanic import Sanic
from sanic.response import json
import redis

app = Sanic()

# request.args['token']

@app.route('/<id>')
async def test(request, id):
    async with redis.StrictRedis(host='0.0.0.0', port=6379, db=0) as r:
        data = await r.get("test")
        # print(data)

    return json({
        'data': data
    })

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=9988)

错误回溯:

[17203] [ERROR] Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sanic/app.py", line 556, in handle_request
    response = await response
  File "main.py", line 11, in test
    async with redis.StrictRedis(host='0.0.0.0', port=6379, db=0) as r:
AttributeError: __aexit__

最佳答案

您应该使用像 aioredis 这样的 redis 库的异步实现。 https://github.com/aio-libs/aioredis

关于Python async redis 给出错误 AttributeError : __aexit__,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51348243/

相关文章:

python-3.x - 在 SymPy 中定义保存数值的符号

redis - 如何使用redis计算所有集合的交集?

python - 没有 while True 循环的 Pygame 播放列表

python - 导入错误 : No module named 'tensorflow.python'

python - python中的while语句

python - 如何填充 numpy 数组的 "partial diagonal axis"

python - 用零替换 numpy 数组中低于阈值 # 的数字

python - 调整图像大小而不失真 OpenCV

java - Redis 为什么需要分布式锁

java - node.js 和 Scala (Java) 之间的连接器