0

我是 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__
4

1 回答 1

-1

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

于 2018-07-16T10:30:13.487 回答