我是 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/