python - Redis - xadd 和 xread 的 Python 示例

标签 python python-3.x redis

你能给出一个在 Python 中使用 Redis 的 xreadxadd 的非常简单的例子吗(它显示了 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/

相关文章:

python - 在 Python 中将秒转换为周-天-时-分-秒

python - 确定为什么一个对象不能被 pickle

python - 将日期时间更改为字符串

python - “dict”对象没有属性 'append'

codeigniter - 如何修复 "redis session expire automatically"

python - PIL简单渐变和TypeError : an integer is required

Python 过程返回值

python - Pandas 按自定义功能分组

amazon-web-services - 使用 RedissonClient 在 AWS redis 中缓存 java POJO

utf-8 - 如何使用redis-cli将utf-8字符设置到redis中?