import redis import time import asyncio import sys async def set_expire_do(i): pool = redis.ConnectionPool(host="host", password="pwd", port=6379, db=i) r = redis.StrictRedis(connection_pool=pool) count = 0 for key in r.scan_iter(): _time = r.ttl(key)
# 没有过期时间 if _time == -1: count += 1 r.expire(key, 1000) if count % 1000 == 0: print('已处理:', count, '条') else: continue print('redis库:', i, "no expire keys {0}".format(count)) async def main(): start = time.time() dbs = sys.argv[1:] if dbs: for db in dbs: await set_expire_do(db) else: for db in range(10): await set_expire_do(db) end = time.time() print("耗时: {0} 秒".format(end - start)) loop = asyncio.get_event_loop() loop.run_until_complete(main()) loop.close()