Python RQ 作业可以重新安排自己并保持依赖作业吗?
Can a Python RQ job reschedule itself and keep depending jobs around?
我有一个 Python 从网络服务器下载资源的 RQ 作业。
如果网络服务器没有响应,下载作业能否自行重新安排并在一定时间间隔后重试下载?
几个转换作业取决于下载作业
job_queue.enqueue(transformation_task, depends_on=download_job)
如果下载作业可以自行重新安排,是否会保留相关作业,并在下载作业完成后最终执行?
我在 python-rq GitHub 项目上问了这个问题,该功能现在包含在 RQ 的 1.5.0 版中。
RQ 现在可让您轻松重试失败的作业。要配置重试,请使用接受 max
和 interval
参数的 RQ 的 Retry 对象。
相关作业将保留在延迟作业注册表中,直到它们所依赖的作业成功并仅在那时执行。
例如:
from redis import Redis
from rq import Queue, Retry
from somewhere import randomly_failing_task, dependent_task
job_queue = Queue(connection=Redis())
randomly_failing_job = job_queue.enqueue(randomly_failing_task, retry=Retry(max=3))
dependent_job = job_queue.enqueue(dependent_task, depends_on=randomly_failing_job)
以及示例任务:
from random import choice
def randomly_failing_task():
print('I am a task, I will fail 50% of the times :/')
success = choice([True, False])
if success:
print('I succeed :)')
else:
print('I failed :(')
raise Exception('randomly_failing_task failed!')
def dependent_task():
print('I depend upon the randomly_failing_task.')
print('I am only executed, once the randomly_failing_task succeeded.’)
我有一个 Python 从网络服务器下载资源的 RQ 作业。
如果网络服务器没有响应,下载作业能否自行重新安排并在一定时间间隔后重试下载?
几个转换作业取决于下载作业
job_queue.enqueue(transformation_task, depends_on=download_job)
如果下载作业可以自行重新安排,是否会保留相关作业,并在下载作业完成后最终执行?
我在 python-rq GitHub 项目上问了这个问题,该功能现在包含在 RQ 的 1.5.0 版中。
RQ 现在可让您轻松重试失败的作业。要配置重试,请使用接受
max
和interval
参数的 RQ 的 Retry 对象。相关作业将保留在延迟作业注册表中,直到它们所依赖的作业成功并仅在那时执行。
例如:
from redis import Redis
from rq import Queue, Retry
from somewhere import randomly_failing_task, dependent_task
job_queue = Queue(connection=Redis())
randomly_failing_job = job_queue.enqueue(randomly_failing_task, retry=Retry(max=3))
dependent_job = job_queue.enqueue(dependent_task, depends_on=randomly_failing_job)
以及示例任务:
from random import choice
def randomly_failing_task():
print('I am a task, I will fail 50% of the times :/')
success = choice([True, False])
if success:
print('I succeed :)')
else:
print('I failed :(')
raise Exception('randomly_failing_task failed!')
def dependent_task():
print('I depend upon the randomly_failing_task.')
print('I am only executed, once the randomly_failing_task succeeded.’)