我有这样的代码
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
我如何获得previus结果?
答案 0 :(得分:4)
我只想找到解决方案。
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项目使用django_rq库的一些代码。只记得Job.key看起来像这样" job:id:job_key"。拆分:并获取第二个元素