python redis pipeline 的使用

con = redis.StrictRedis(host='localhost', port=6379, db=2)
redis_pip = con.pipeline(transaction=False)


file_name = '**.txt'
batch_size = 1000
with open(file_name, 'r', encoding='utf-8') as file:
count = 0
for lines in file:
lines = lines.strip()
line_list = lines.split('\t')
user_id = line_list[0]
value = lines
count = count + 1
if lines:
redis_pip.hset('**', user_id, value)
if not count % batch_size == 0:
print("flush")
try:
redis_pip.execute()
except Exception as e:
print(e)
count = 0
redis_pip.execute()

猜你喜欢

转载自www.cnblogs.com/love-chen-forever/p/11759974.html