Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis-py hset() mapping - TypeError when setting multiple item values

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.

like image 590
Harry Avatar asked Apr 26 '26 08:04

Harry


2 Answers

This appears to be a known issue as shown here --> https://github.com/andymccurdy/redis-py/issues/1310#issuecomment-603081122.

As you can see in that image linked, the source code in PyPi has hset with a function signature that does not include the keyword mapping. You should verify in your installation of py-redis that the same issue is present and follow that ticket as well. To work around it you can clone straight from master branch in order to use that feature.

like image 139
gold_cy Avatar answered Apr 28 '26 20:04

gold_cy


Updating with

pip install -U redis

solved the issue for me.

like image 43
CGFoX Avatar answered Apr 28 '26 20:04

CGFoX



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!