Python异步redis给出错误AttributeError:__aexit__

时间:2018-07-15 12:10:38

标签: 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__

1 个答案:

答案 0 :(得分:0)

您应该使用Redis库的异步实现,例如aioredis。 https://github.com/aio-libs/aioredis