1

我只是在学习 redis 并写了一个小片段来测试 redis 数据库的连接、写入和读取。这是脚本:

import random
import redis
from datetime import datetime

random.seed(1)
temps = "t"
humids = "h"

# create connection to redis server on default port 6379
POOL = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)
redis = redis.StrictRedis(connection_pool=POOL)

x = 0
while x < 513:
    y = random.random()
    now = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')
    # next line to cut off timestamp at millisecond precision
    # now = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
    redis.hset(temps, str(now)+"_t", y)
    redis.hset(humids, str(now)+"_h", y)
    x += 1

# print(redis.hgetall(sensor_log))
print("****************************************")
print("all temps:\n")
print(redis.hvals(temps))
print("****************************************")
print("all humids:\n")
print(redis.hvals(humids))
print("****************************************")
print("done")

该片段(显然)创建一个随机数并将其写入两个 redis 哈希中。

这是奇怪的行为:

当我运行循环512次(即while x < 512时,两个散列中的值是相同的(它们应该是)。

但是,当我运行循环513次(即while x < 513时,两个散列中的值突然彼此不同。有时,两个散列中的第一个值相同,但随后的所有值都彼此不同。

有人可以解释一下吗?

这是我运行它的环境:

  • 树莓派 4
  • Python 3.7.3
  • Redis 服务器 v=5.0.3 sha=00000000:0 malloc=jemalloc-5.1.0 bits=32 build=afa0decbb6de285f

两个哈希值的屏幕截图,运行 512 次

运行 513 次的两个哈希值的屏幕截图

在 513 循环运行的两个哈希值的粘贴箱

4

1 回答 1

0

两个散列中的值相同的 - 只是在超过 512 个条目之后 redis.hvals 打印出来的顺序突然不同,从而导致输出的视觉不匹配。

大卫做对了(见问题后他的第一条评论)。

于 2020-04-03T01:32:44.983 回答