Python-redis: get binary data after a client was set up with decode_responses=True

后端 未结 1 1668
孤城傲影
孤城傲影 2021-01-27 14:41

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

相关标签:
1条回答
  • 2021-01-27 15:22

    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...

    def new_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))
    
    ...
    
    # later
    with new_client(client, decode_responses=False) as binclient:
        data = binclient.get(name)
    stuff = pickle.loads(data)
    
    0 讨论(0)
提交回复
热议问题