Celery查看任务执⾏状态
使⽤AsyncResult就可以通过id对应任务的执⾏状态了
sult import AsyncResult
task = AsyncResult("your-task-id")
status = task.status
但是看第⼆个回答,确实也是我的⼀⼤问题,如果在执⾏状态还是显⽰PENDING。另外如果task_id根本不到,celery也是返回PENDING的。作者说的是要有⼀个独⽴于Celery的存储,我这边是使⽤了MongoDB存储各个任务id,然后成功或者失败的话再根据对应id去更新你的数据库。
最后的restful服务写法如下:
from flask import Flask
from flask_restful import Resource, reqparse, Api
from gevent import pywsgi
flask_app = Flask(__name__)
fig.update(RESTFUL_JSON=dict(ensure_ascii=False))# flask config change unicode to chinese
flask_api = Api(flask_app)
class TaskResultGet(Resource):
def get(self):
return self.post()
def post(self):
result ={'response':{}}
parser = reqparse.RequestParser(bundle_errors=True)
parser.add_argument('task_id',type=str, required=True)
args = parser.parse_args(strict=False)
task = get_task(args['task_id'])# get task from mongodb
if len(task)==0:
result['response']['success']=0
pendingresult['response']['msg']='TASK NOT FOUND'
else:
result['response']['success']=1
database_task_status = task[0]['task_status']
if database_task_status is not None:
if database_task_status =='SUCCESS':
result['response']['msg']= database_task_status
result['response']['details']= task[0]['result']
elif database_task_status =='FAILURE':
result['response']['success']=0
result['response']['msg']= database_task_status
result['response']['details']= task[0]['result']
else:
celery_task_status = AsyncResult(args['task_id']).status  # get task status from celery
result['response']['msg']= celery_task_status
if celery_task_status =='PENDING':
result['response']['msg']+=','+ get_all_type_pending_tasks(args['task_id'])# show the tasks in front of this task return result'
flask_api.add_resource(TaskResultGet,'/get_task_result')
if __name__ =='__main__':
port_id =8470
app_server = pywsgi.WSGIServer(('', port_id), flask_app)
print('restful server start, open port '+str(port_id)+' ...')
app_server.serve_forever()

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。