我有这样的代码
from rq import Queue, use_connection from task import add from redis import Redis import time import logging # use redis by default # create work queue redis_conn = Redis() q = Queue(connection=redis_conn) #notice: cann't run a task function in __main__ module #because rq save module and function name in redis #when rqworker running, __main__ is another module # enqueue tasks,function enqueue returns the job instance job = q.enqueue(add, 3, 9) job = q.enqueue(add, 4, 9) job = q.enqueue(add, 5, 9) job = q.enqueue(add, 6, 9) time.sleep(3) #get the job result by job.result logging.warn("result is %s", job.result)
当我运行应用程序时,我得到这样的输出
WARNING:root:result is 15
如何获得上一个结果?
我只是想办法。
def recommend(request): user_id = int(request.GET.get('user_id','287')) job = django_rq.enqueue(recommended,user_id) # Enqueue Job logger.debug(job.key) response_data = {'status':'processing', 'job': job.key} return HttpResponse(json.dumps(response_data), content_type="application/json") def get_result(request): job_id = request.GET.get('job_id') redis_conn = django_rq.get_connection() job_id=job_id.split(':')[2] logger.info(job_id) job = Job.fetch(job_id,redis_conn) #fetch Job from redis if job.is_finished: ret = job.return_value elif job.is_queued: ret = {'status':'in-queue'} elif job.is_started: ret = {'status':'waiting'} elif job.is_failed: ret = {'status': 'failed'} return HttpResponse(json.dumps(ret), content_type="application/json")
这是我使用django_rq库的Django项目中的一些代码。只要记住Job.key看起来像这样的“ job:id:job_key”。除以:并获得第二个元素