简体   繁体   English

为什么AsyncResult在任务完成后返回NoneType?

[英]Why AsyncResult returns NoneType after task is done?

Problem 问题

AsyncResult returns NoneType for task after it is finished. 完成后,AsyncResult为任务返回NoneType。

Question

How can I get celery task state after it is done? 完成后如何获取芹菜任务状态?

Description 描述

Below is a code of django view for getting celery task state. 下面是Django视图的代码,用于获取celery任务状态。 I use this view to query task progress from my client app. 我使用此视图从客户端应用程序查询任务进度。 It works fine, until the task is done. 正常运行,直到完成任务。 Afterwards AsyncResult returns NoneType each time I call it for my task, and i get the following error: 之后,每次我为任务调用AsyncResult时,它都会返回NoneType,并且出现以下错误:

'NoneType' object has no attribute 'get' 'NoneType'对象没有属性'get'

# Celery configuration
celery = Celery('tasks', backend='amqp', broker="amqp://")

# My django view for getting task state    
def GetTaskStatus(request):
    task = AsyncResult(request.body["taskid"], app=celery)
    if task.state == 'PENDING':
        data = {
            'state': task.state,
            'progress': task.info.get("progress", 0)
        }
    elif task.state != 'FAILURE':
        data = {
            'state': task.state,
            'progress': task.info.get("progress", 0)
        }
    else:
        data = {
            'state': task.state,
            'error': "something went wrong",
            'progress': task.info.get("progress", 0)
        }
    response = GetHttpResponseJSON(ResponseCode.OK, data)
    return response

You need to define a result backend. 您需要定义一个结果后端。 See the tutorial . 请参阅教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM