redis-py icon indicating copy to clipboard operation
redis-py copied to clipboard

Request for supporting xread returning a list instead of dict

Open chill1n opened this issue 2 years ago • 2 comments

version: redis 4.4.0 platform: Python 3.8 on Ubuntu 20.04

For reasons of (memory and time) efficiency we are streaming values to redis as raw numbers instead of key-value pairs, using xadd. Unfortunately, python redis only supports returning the list of values as a dictionary of key-value pairs. For our use case we would need an option to have xread return just a list of values/strings.

I checked out the code and found the location where the mapping to key-value pairs is performed, but I'm wondering what the best generic method would be to pass this option to xread (or perhaps a config option of the Redis object). Any ideas are appreciated.

Example to reproduce the issue is given below:

import os
import redis

REDIS_URL = os.environ.get("REDIS_URL", "127.0.0.1:6379")
rds = redis.StrictRedis.from_url(f'redis://{REDIS_URL}')

"""
redis-cli xadd 99:8:1:40 1672345126328633-0 0.950178 8.667000 8.668000 8.662473 8.662473 154.934304 -202.148236 8.665024 8.662473 8.662473 8.662473 0.000000
redis-cli xread count 1 streams 99:8:1:40 0-0
1) 1) "99:8:1:40"
   2) 1) 1) "1672345126328633-0"
         2)  1) "0.950178"
             2) "8.667000"
             3) "8.668000"
             4) "8.662473"
             5) "8.662473"
             6) "154.934304"
             7) "-202.148236"
             8) "8.665024"
             9) "8.662473"
            10) "8.662473"
            11) "8.662473"
            12) "0.000000"

"""
channel = "99:8:1:40"
data = [ 0.950178, 8.667000, 8.668000, 8.662473, 8.662473, 154.934304, -202.148236, 8.665024, 8.662473, 8.662473, 8.662473, 0.000000 ]

events = rds.xread(streams={channel: '0-0'}, count=1)
print(events)

# Returns: [[b'99:8:1:40', [(b'1672345126328633-0', {b'0.950178': b'8.667000', b'8.668000': b'8.662473', b'8.662473': b'0.000000', b'-202.148236': b'8.665024'})]]]
# instead of 
# [[b'99:8:1:40', [(b'1672345126328633-0', [b'0.950178', b'8.667000', b'8.668000', b'8.662473', b'8.662473', b'154.934304', b'-202.148236', b'8.665024', b'8.662473', b'8.662473', b'8.662473', b'0.000000' ]] )]]]
# or
# [[b'99:8:1:40', [(b'1672345126328633-0', [(b'0.950178', b'8.667000)', b'(8.668000', b'8.662473)', b'(8.662473', b'154.934304)', b'(-202.148236', b'8.665024)', b'(8.662473', b'8.662473)', b'(8.662473', b'0.000000') ]] )]]]

chill1n avatar Dec 29 '22 22:12 chill1n