python redis-queue:来自documentaion的简单示例不起作用

时间:2018-11-30 22:35:10

标签: python python-3.x redis queue message-queue

我有两个文件,实际上是从http://python-rq.org/docs/复制粘贴而成的:

app.py

    from rq import Queue
    from redis import Redis
    from somewhere import count_words_at_url
    import time

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

    print(redis_conn)
    # Delay execution of count_words_at_url('http://nvie.com')
    job = q.enqueue(count_words_at_url, 'http://nvie.com')
    print(job.result)   # => None

    # Now, wait a while, until the worker is finished
    time.sleep(10)
    print(job.result)   # => 889

somewhere.py

import requests
def count_words_at_url(url):
    print("hello?")
    resp = requests.get(url)
    return len(resp.text.split())

我运行了app.py,我得到的输出是2 None值,而根据文档我应该得到889。

我不确定我为什么会这样。我的超时时间是10秒,比文档中的时间更长,因此我期望这项工作能够完成。

我在做什么错?

1 个答案:

答案 0 :(得分:1)

  1. Redis服务器是否正在运行?

    服务Redis服务器状态

  2. rq作业程序正在运行吗?

    rq信息

如果没有工人在奔跑,那么

rq worker # run this under the same directory of your project
18:44:54 RQ worker 'rq:worker:ubuntu.45276' started, version 0.12.0
18:44:54 *** Listening on default...
18:44:54 Cleaning registries for queue: default
  1. 用更简单的功能(例如

    )替换count_words_at_url

    def just_mock(url): time.sleep(5) 返回“ {}的单词数是??”。format(url)