【问题标题】:python redis-queue: Simple example from documentaion not workingpython redis-queue:来自文档的简单示例不起作用
【发布时间】:2019-05-03 02:22:46
【问题描述】:

我有两个文件,实际上是从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 秒,它比文档中的时间长,所以我期待这项工作已经完成。

我做错了什么?

【问题讨论】:

  • 隔离,count_words_at_url('http://nvie.com') 自己返回 889 吗?

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


【解决方案1】:
  1. Redis 服务器是否正在运行?

    服务 redis-server 状态

  2. rq worker 正在运行吗?

    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): 时间.sleep(5) return "count words for {} is ??".format(url)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-01
    • 2015-11-23
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-11
    • 1970-01-01
    相关资源
    最近更新 更多