gpt4 book ai didi

python rq 作业队列 - time.sleep()

转载 作者:行者123 更新时间:2023-11-28 22:46:20 24 4
gpt4 key购买 nike

我希望使用 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 是否仍然适用于此类作业 - 执行时间差异很大?

任何建议都会很棒!

最佳答案

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

在您引用的示例中,显然假设 count_words_at_url 没有给出关于何时完成的明确指示(您可以通过在 上循环“轮询”,而 job.result 为 None:while 的主体中有一个 sleep,但这非常脆弱——尽可能避免轮询。

关于python rq 作业队列 - time.sleep(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27625977/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com