Search code examples
pythonredissetpipeline

Python, redis: How do I set multiple key-value pairs at once


I have two lists keys= [k0,k1, ....kn] vals= [v0,v1, ....vn]

I can set these key-values on redis in multiple steps doing the following: for i in range(0,len(keys)): redis_con.set(keys[i], vals[i]) But this is multiple set operations. How can I do this in one async step?


Solution

  • Assuming you want a single redis call for set ops:

    pipe = redis_con.pipeline()
    for i in range(0,len(keys)):
      pipe.set(keys[i], vals[i])
    pipe.execute()