【python】python利用pipeline操作redis

#python利用pipeline操作redis

import redis

NUM = 10000 # execute every 10000 command
field = "filed"
value = "value"

def run():
    redis_host = "1.1.1.1"
    redis_port = 6379
    redis_cli = redis.StrictRedis(host=redis_host, port=redis_port, db=0)
    pipe = redis_cli.pipeline()

    count = 0
    try:
        f_handle = open("./host.file", 'r')  #read hostlist from local file
        lines = f_handle.readlines()
        i = 0
        for line in lines:
            key = "key" + line[:-1]
            pipe.exists(key)
            i = i + 1

            if i % NUM == 0:
                i = 0
                ret = pipe.execute() 
                #print(len(ret))
                count = count + sum(ret)

        if i != 0:
            ret = pipe.execute()
            #print (len(ret))
            count = count + sum(ret)
    except Exception as ex:
        print ex

    finally:
        if f_handle:
            f_handle.close()     
    return count

if __name__ == "__main__":
    count = run()

你可能感兴趣的:(Python,redis)