我只是在学习 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