I am building some very large lookup tables in Redis. I have some straightforward code that works as intended when looping through a dict to set a single value in my Redis hash (via a pipeline) for each item using hset()
:
foo = {"1234": "5678", "abcd": "efgh", ... }
with self.db.pipeline() as pipe:
for foo in bar:
pipe.hset("lookup_table", foo["key"], foo["value"])
pipe.execute()
This is slow with a large dict. To speed it up, I want to be able to set multiple items as a mapping into the pipeline without having to loop over it. With hmset()
now deprecated, it seems that hset()
can accept a mapping via a keyword arg. I have attempted to do the following:
with self.db.pipeline() as pipe:
pipe.hset("lookup_table", mapping=foo)
pipe.execute()
but this yields the error TypeError: hset() got an unexpected keyword argument 'mapping'
. Am I using hset()
incorrectly? Or am I mistaken in thinking that hset()
can accept multiple items in this way?
I'm using py-redis 3.4.1 with Python 3.7.5.