【发布时间】:2018-06-14 04:31:38
【问题描述】:
我在我的项目中使用django-rq。
我想要达到的目标: 我有一个加载模板的第一个视图,该模板从网络摄像头获取图像并保存在我的电脑上。然后,视图调用第二个视图,其中使用 rq 将处理图像的异步任务排入队列。最后,在 20 秒延迟后,调用第三个视图。在后一种视图中,我想检索异步任务的结果。
问题:作业对象已正确创建,但队列始终为空,所以无法使用queue.fetch_job(job_id)。阅读here 我设法在 FinishedJobRegistry 中找到了工作,但我无法访问它,因为注册表不可迭代。
from django_rq import job
import django_rq
from rq import Queue
from redis import Redis
from rq.registry import FinishedJobRegistry
redis_conn = Redis()
q = Queue('default',connection=redis_conn)
last_job_id = ''
def wait(request): #second view, starts the job
template = loader.get_template('pepper/wait.html')
job = q.enqueue(processImage)
print(q.is_empty()) # this is always True!
last_job_id = job.id # this is the expected job id
return HttpResponse(template.render({},request))
def ceremony(request): #third view, retrieves the result
template = loader.get_template('pepper/ceremony.html')
print(q.is_empty()) # True
registry = FinishedJobRegistry('default', connection=redis_conn)
finished_job_ids = registry.get_job_ids() #here I have the correct id (last_job_id)
return HttpResponse(template.render({},request))
问题:如何从已完成的作业注册表中检索异步作业的结果?或者,更好的是,我怎样才能正确地将作业排入队列?
【问题讨论】:
标签: python django python-rq django-rq