2

我正在寻找使用 RQ 运行排队作业,但查看以下示例:

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

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

# 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(2)
print job.result   # => 889

我明白time.sleep(2)了 - 我想知道这是否必须指定。我安排的工作可能需要(有时)一个小时才能完成(这因每个工作而异)。

RQ 是否仍然适用于执行时间差异很大的此类工作?

任何建议都会很棒!

4

1 回答 1

4

只要您安排的每项工作都给出了一些明确的、可观察到的指示,表明一切都已完成,您绝对可以使用 RQ 并等待此类“指示”;那么您将依靠这些指示来判断何时访问每个作业的result.

在您引用的示例中,显然假设count_words_at_url没有明确指示何时完成(您可以通过在 ' 主体中循环来“轮询” while job.result is None:sleep但这while非常脆弱-尽可能避免轮询)。

于 2014-12-23T18:27:59.000 回答