我找不到一个合适的方法,所以我做了一点小改动:
from redis import Redis
class MyRedis(Redis):
lua_retry_time = 120
# override execute to retry busy errors
def execute_command(self, *args, **options):
wait_time = 0
if not self.lua_retry_time:
return super().execute_command(*args, **options)
while wait_time < self.lua_retry_time:
try:
return super().execute_command(*args, **options)
except ResponseError as e:
if 'busy redis is busy' not in ''.join(e.args).lower():
raise e
if wait_time == 0: # only print once
print('Redis is busy, waiting up to 120 seconds...')
time.sleep(2)
wait_time += 2
return super().execute_command(*args, **options)
每一个命令
redis-py
包裹通过
execute_command
方法这应该涵盖所有内容。