Last active
December 22, 2016 10:20
-
-
Save tawateer/0417799ed2a843ce589b520dd7cea875 to your computer and use it in GitHub Desktop.
扫描 redis key 并删除部分 key。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
import urllib2 | |
from multiprocessing.dummy import Pool as ThreadPool | |
import redis | |
hosts_d = { | |
"host1": [6360, 6361, 6362, 6363], | |
"host2": [6501], | |
"host3": [6501] | |
} | |
def get_redis_obj(host, port, db=0): | |
return redis.StrictRedis(host=host, port=port, db=db) | |
def single(c): | |
redis_oj = get_redis_obj(c["host"], c["port"]) | |
index = 0 | |
while True: | |
ret = redis_oj.scan(index) | |
index = ret[0] | |
for k in ret[1]: | |
ttl = redis_oj.ttl(k) | |
if ttl == -1: | |
print c["host"], c["port"], k, ttl, redis_oj.delete(k) | |
if index == 0: | |
break | |
def main(): | |
hosts_l = [] | |
for host, ports in hosts_d.items(): | |
for port in ports: | |
hosts_l.append( | |
{ | |
"host": host, | |
"port": port | |
} | |
) | |
pool = ThreadPool(len(hosts_l)) | |
results = pool.map(single, hosts_l) | |
pool.close() | |
pool.join() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment