redis // redis-hash 在传递 512 个哈希条目后被破坏
redis // redis-hash gets broken after passing 512 hash entries
我刚刚在学习 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
)两个散列中的值突然彼此不同。有时两个哈希中的第一个值相同,但随后的所有值都彼此不同。
谁能解释一下?
这是我运行正在使用的环境:
- Raspberry Pi 4
- Python 3.7.3
- Redis 服务器 v=5.0.3 sha=00000000:0 malloc=jemalloc-5.1.0 bits=32
构建=afa0decbb6de285f
Screenshot of values in both hashes with 512 runs
Screenshot of values in both hashes with 513 runs
两个散列中的值是相同的 - 只是在超过512个条目后redis.hvals打印出来的顺序突然不同,从而导致视觉输出不匹配。
大卫答对了(见他在问题后的第一条评论)。
我刚刚在学习 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
)两个散列中的值突然彼此不同。有时两个哈希中的第一个值相同,但随后的所有值都彼此不同。
谁能解释一下?
这是我运行正在使用的环境:
- Raspberry Pi 4
- Python 3.7.3
- Redis 服务器 v=5.0.3 sha=00000000:0 malloc=jemalloc-5.1.0 bits=32 构建=afa0decbb6de285f
Screenshot of values in both hashes with 512 runs
Screenshot of values in both hashes with 513 runs
两个散列中的值是相同的 - 只是在超过512个条目后redis.hvals打印出来的顺序突然不同,从而导致视觉输出不匹配。
大卫答对了(见他在问题后的第一条评论)。