Skip to content Skip to sidebar Skip to footer

Python-redis: Get Binary Data After A Client Was Set Up With Decode_responses=true

So, following the excellent suggestion in both this answer and that answer, I decided to replace a whole bunch of encode/decode to/from UTF-8 all over the place by a single: rdb =

Solution 1:

Here is what I came up with. Not sure how it would handle complex connections and what else it may break. Just don't run your self-driving car with that...

defnew_client(client, **kwargs):
    """return a new Redis client based on an existing one,
    with some kwargs modified.
    """
    kwargs = {**client.connection_pool.connection_kwargs, **kwargs}
    return redis.StrictRedis(**kwargs)

With this, now we can do, e.g.:

client.set(name, pickle.dumps(stuff))

...

# laterwithnew_client(client, decode_responses=False) as binclient:
    data = binclient.get(name)
stuff = pickle.loads(data)

Post a Comment for "Python-redis: Get Binary Data After A Client Was Set Up With Decode_responses=true"