你能给出一个在 Python 中使用 Redis 的 xread
和 xadd
的非常简单的例子吗(它显示了 xread 返回值的类型和格式以及 xadd 的输入) ?我已经阅读了很多文档,但没有一个是用 Python 编写的。
Redis doc举个例子:
> XADD mystream * sensor-id 1234 temperature 19.8
1518951480106-0
但是如果我在 python 中尝试:
sample = {b"hello":b"12"}
id = r.xadd("mystream", sample)
我收到这个错误:
redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value
最佳答案
确保在运行之前刷新,以确保不存在同名的列表/流。 :
redis-cli flushall
if __name__ == '__main__':
r = redis.Redis(host='localhost', port=6379, db=0)
encoder = JSONEncoder()
sample = {"hello": encoder.encode([1234,125, 1235, 1235])} # converts list to string
stream_name = 'mystream'
for i in range(10):
r.xadd(stream_name, sample)
# "$" doesn't seem to work in python
read_samples = r.xread({stream_name:b"0-0"})
关于python - Redis - xadd 和 xread 的 Python 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55019298/