REDIS:python中的redis不会返回任何内容

时间:2017-06-13 05:30:24

标签: python pandas redis

我正在尝试使用redis与API(Mailchimp)进行通信,并将查询结果保存到csv文件中;但该函数既不保存csv文件也不返回任何错误。我错过了什么?

这是我的bg功能:

def mailchimp_get_subscribed():

    config = mailchimp_config()
    path_params = 'lists' + '/xxxxxxxxx' + '/members'
    endpoint = urlparse.urljoin(config.api_root, path_params)
    query_params = {'fields': 'members.email_address', 'count': 10, 'offset': 0}

    data = requests.get(endpoint, auth=('apikey', config.apikey), params=query_params, verify=False).text
    data = json.loads(data)
    df = json_normalize(data['members'])
    df.to_csv('existing.csv')

    return df

这是我的main.py:

from rq import Queue
from redis import Redis
from mailchimp_get_subscribed import mailchimp_get_subscribed

# Tell RQ what Redis connection to use
redis_conn = Redis()
q = Queue(connection=redis_conn) # no args implies the default queue

# Delay execution of mailchimp_get_subscribed
job = q.enqueue(mailchimp_get_subscribed)
print(job.result)
time.sleep(5)
print(job.result)

这是输出:

runfile('/path/main.py', wdir='/path')
Reloaded modules: mailchimp_get_subscribed, mailchimp_config
None
None

尽管api响应需要一毫秒,但我的main.py既没有返回df,也没有保存csv文件。我已经尝试执行mailchimp_get_subscribed()并将csv正确保存在当前路径上,因此当redis没有执行时,该函数正常工作。

我错过了什么?

在adition中......这是redis-cli监视器显示的日志:

1497331842.335658 [0 [::1]:59600] "MULTI"
1497331842.335722 [0 [::1]:59600] "SADD" "rq:queues" "rq:queue:default"
1497331842.335762 [0 [::1]:59600] "HSET" "rq:job:afe583b0-82e1-45ae-830b-9564faaa932e" "status" "queued"
1497331842.335776 [0 [::1]:59600] "HMSET" "rq:job:afe583b0-82e1-45ae-830b-9564faaa932e" "timeout" "180" "description" "mailchimp_get_subscribed.mailchimp_get_subscribed()" "data" "\x80\x04\x95<\x00\x00\x00\x00\x00\x00\x00(\x8c1mailchimp_get_subscribed.mailchimp_get_subscribed\x94N)}\x94t\x94." "status" "queued" "enqueued_at" "2017-06-13T05:30:42Z" "created_at" "2017-06-13T05:30:42Z" "origin" "default"
1497331842.335827 [0 [::1]:59600] "RPUSH" "rq:queue:default" "afe583b0-82e1-45ae-830b-9564faaa932e"
1497331842.335862 [0 [::1]:59600] "EXEC"
1497331842.336224 [0 [::1]:59600] "HGET" "rq:job:afe583b0-82e1-45ae-830b-9564faaa932e" "result"
1497331847.342004 [0 [::1]:59600] "HGET" "rq:job:afe583b0-82e1-45ae-830b-9564faaa932e" "result"

2 个答案:

答案 0 :(得分:1)

Bingo ..我遇到了问题

您正在使用命令设置哈希:

"HMSET" "rq:job:afe583b0-82e1-45ae-830b-9564faaa932e" "timeout" "180" "description" "mailchimp_get_subscribed.mailchimp_get_subscribed()" "data" "\x80\x04\x95<\x00\x00\x00\x00\x00\x00\x00(\x8c1mailchimp_get_subscribed.mailchimp_get_subscribed\x94N)}\x94t\x94." "status" "queued" "enqueued_at" "2017-06-13T05:30:42Z" "created_at" "2017-06-13T05:30:42Z" "origin" "default"

如果我获得以下数据:rq:job:afe583b0-82e1-45ae-830b-9564faaa932e

127.0.0.1:6379> HGETALL rq:job:afe583b0-82e1-45ae-830b-9564faaa932e
 1) "data"
 2) "\x80\x04\x95<\x00\x00\x00\x00\x00\x00\x00(\x8c1mailchimp_get_subscribed.mailchimp_get_subscribed\x94N)}\x94t\x94."
 3) "description"
 4) "mailchimp_get_subscribed.mailchimp_get_subscribed()"
 5) "origin"
 6) "default"
 7) "enqueued_at"
 8) "2017-06-13T05:30:42Z"
 9) "timeout"
10) "180"
11) "created_at"
12) "2017-06-13T05:30:42Z"
13) "status"
14) "queued"

您正尝试使用以下方法获取哈希数据:

"HGET" "rq:job:afe583b0-82e1-45ae-830b-9564faaa932e" "result"

但是没有任何内容result在你的哈希中

这就是为什么你得到blanknull的结果。

尝试以下代码进行修复:

print(job.description)

OR

print(job.enqueued_at)

答案 1 :(得分:1)

问题是我没有启动工人!