连接我自己 sanic-redis
app = Sanic()
redis = SanicRedis(app)
app.config.update(
{
'REDIS': {
'address': ('0.0.0.0', 6379),
# 'db': 0,
# 'password': 'password',
# 'ssl': None,
# 'encoding': None,
'minsize': 1,
'maxsize': 10
}
}
)
我增加了每个请求的错误或成功计数器,但是对于每个请求,我都会捕获以下错误:
Connection <RedisConnection [db:0]> has pending commands, closing it.
Future exception was never retrieved
future: <Future finished exception=ConnectionForcedCloseError()>
aioredis.errors.ConnectionForcedCloseError
Future exception was never retrieved
future: <Future finished exception=ConnectionForcedCloseError()>
aioredis.errors.ConnectionForcedCloseError
同时,一切正常,没有磨损,总的来说,一切都很好。
问题:如何用萝卜打开非关闭连接?
async def count_statistic():
await redis_client.incr_success_and_all_count_in_redis()
now = time.time()
last_timestamp = await redis_client.get_timestamp_from_redis()
redis_client.time_to_response = now - redis_client.time_start_request
await redis_client.incr_sum_time_response_in_redis(redis_client.time_to_response)
await redis_client.put_timestamp_to_redis(now)
ttl = await redis.conn.pttl('average_stats')
if (now - last_timestamp < 60):
try:
all_count, sum_time_response = await redis_client.get_for_statistic()
first_average = sum_time_response / all_count
except TypeError:
return 0
with await redis.conn as r:
r.hset(redis_client.key, 'first_average', first_average)
r.hset('average_stats', 'average_per_minute', first_average)
await redis.conn.expire('average_stats', 120)
else:
await redis_client.drop_data()
@andreymal在评论中出色地处理了这个问题。以前缺乏
await
。hset
再次,粗心是原因。