无法从redis-cli检索python redis写入的数据

时间:2017-10-19 17:26:23

标签: python redis celery

我有以下代码:

#!/usr/bin/python2
from sp_polling import SPPoller
from celery import Celery
import traceback
import logging
import threading
from orchestrators import ORCHESTRATORS
import redis

REDIS_CONN = redis.Redis(host='localhost', port=6379, db=0)
ALL_ALARMS = {}

app = Celery("main", backend="redis://localhost:6379/0", broker="amqp://")

@app.task
def dispatch_task(customer, details):
    ...
    ...
    return data

def get_alarms():
    global ALL_ALARMS
    for orchestrator in ORCHESTRATORS:
        results = dispatch_task.delay(orchestrator)
        ALL_ALARMS.update(results.get())
    REDIS_CONN.hmset("all_alarms", ALL_ALARMS)
    threading.Timer(300, get_alarms).start()

if __name__ == "__main__":
    get_alarms()

当我尝试通过redis-cli连接并检索数据时,它一直告诉我空集:

myuser@myhost:~/my-docs$ redis-cli
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> keys *
(empty list or set)
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379>

我已经选择了每个数据库整数为15,并且所有数据库都以(空列表或集合)回复。我知道数据正在某处保存,因为当我执行以下交互式python会话时,我可以看到正在填充的数据:

myuser@myhost:~/my-docs$ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
>>> import redis
>>> REDIS_CONN = redis.Redis(host='localhost', port=6379, db=1)
>>> REDIS_CONN.flushall()
True
>>> REDIS_CONN.hgetall("all_alarms")
{}

5分钟后......

>>> REDIS_CONN.hgetall("all_alarms")
{'data': "losts of data",
...
...
}

我已经读过它可能与Redis中的数据库实例有关,但我还没有弄清楚如何连接到特定实例(如果我有多个实例)。

非常感谢任何帮助!

0 个答案:

没有答案