redis-py and hgetall behavior

python redis password
python redis get all keys
python redis-lock
python redis hmset
python redis pubsub
redis exists python
redis json python
python redis hmget

I played around with flask microframework, and wanted to cache some stats in redis. Let's say I have this dict:

mydict = {}
mydict["test"] = "test11"

I saved it to redis with

redis.hmset("test:key", mydict)

However after restore

stored = redis.hgetall("test:key")
print(str(stored))

I see weird {b'test': b'test11'} so stored.get("test") gives me None

mydict str method result looks fine {'test': 'test11'}. So, why this binary marker added to restored data? I also checked in redis-cli and don't see explicit b markers there. Something wrong with hgetall?

Welcome to redis-py's documentation! — redis-py 2.10.5 , The Python example used the redis-py methods hmset(),hset(),hget() To avoid blocking behavior the commands SCAN and HSCAN can be used which allow� This also means that HGETALL becomes r.hgetall(), PING becomes r.ping(), and so on. There are a few exceptions, but the rule holds for the large majority of commands. While the Redis command arguments usually translate into a similar-looking method signature, they take Python objects.

POOL = redis.ConnectionPool(host='localhost', **decode_responses=True**, port=6379, db=0)
datastore = redis.StrictRedis(connection_pool=POOL)

if you use the ConnectionPool, you should move the decode_responses=True to the constructor of ConnectionPool.

Redis Hash, __getitem__() to mimic Redis' behavior of returning a null-like value when no key Using HMSET is probably a closer parallel for the way that we assigned data� The Redis command HGETALL retrieves all the keys and their values present in a hash. The redis-py returns the keys and values as a Python dictionary. The Redis command HKEYS retrieves all the keys present in a hash. The redis-py returns the keys as a Python list. The Redis command HVALS retrieves all the keys present in a hash.

Here's a simple routine to perform the byte decoding for the redis dict returned by HGETALL

def decode_redis(src):
    if isinstance(src, list):
        rv = list()
        for key in src:
            rv.append(decode_redis(key))
        return rv
    elif isinstance(src, dict):
        rv = dict()
        for key in src:
            rv[key.decode()] = decode_redis(src[key])
        return rv
    elif isinstance(src, bytes):
        return src.decode()
    else:
        raise Exception("type not handled: " +type(src))

... and to use, just pass it the redis dict ...

obj = decode_redis(redis_connection.hgetall(key))

How to Use Redis With Python – Real Python, Here is listing: Python 3.3.0 (default, Dec 19 2013, 23:07:15) [GCC redis 127.0. 0.1:6379> hgetall test:key This is the intended behavior. redis 127.0.0.1:6379> hgetall test:key "test" "test11" redis-py version is 2.9.1, installed via pip, redis-server version is 2.2.12 This is the intended behavior

Problem with hash get/set � Issue #463 � andymccurdy/redis-py , When use get(), megt(), keys() etc, redis-py always return bytestring. And if someone is searching specifically for the behavior or setting,� redis-py 3.0 drops support for the pipeline-based Lock and now only supports the Lua-based lock. In doing so, LuaLock has been renamed to Lock. This also means that redis-py Lock objects require Redis server 2.6 or greater. 2.X users that were explicitly referring to “LuaLock” will have to now refer to “Lock” instead.

andymccurdy/redis-py, i played around flask microframework, , wanted cache stats in redis. let's have dict: mydict = {} mydict["test"] = "test11". i saved redis with HGETALL key Get all the fields and values in a hash; HINCRBY key field increment Increment the integer

python 3.x - redis-py and hgetall behavior - - GHI, This abstract class provides a Python interface to all Redis commands and an implementation of the Redis protocol. hgetall(name) Return a new Lock object using key name that mimics the behavior of threading.Lock. Increments the number stored at field in the hash stored at key by increment.If key does not exist, a new key holding a hash is created. If field does not exist the value is set to 0 before the operation is performed.

Comments
  • If someone interested, here is answer from developer: github.com/andymccurdy/redis-py/issues/…
  • This argument is undocumented and it doesn't seems to work: python3 >>> import redis >>> POOL = redis.ConnectionPool(host='localhost', port=6379, db=0) >>> datastore = redis.StrictRedis(connection_pool=POOL, decode_responses=True) >>> mydict = {} >>> mydict["test"] = "test11" >>> datastore.hmset("test:key", mydict) True >>> stored = datastore.hgetall("test:key") >>> stored {b'test': b'test11'}
  • Well thank you pal, but this question is 5.5 years old.
  • @Tommi, understood, but can still be helpful to others with same problem