AsyncResult(task_id)返回“ PENDING”在任务开始后也要声明 [英] AsyncResult(task_id) returns "PENDING" state even after the task started

查看:259
本文介绍了AsyncResult(task_id)返回“ PENDING”在任务开始后也要声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在项目中,我尝试轮询长时间运行的任务的task.state并更新其运行状态。它可以在开发中使用,但是当我在生产服务器上移动项目时将无法使用。即使看到任务开始于花,我也一直保持 PENDING状态。但是,我仍然可以在任务完成时(task.state =='SUCCESS')更新结果。我在生产中使用了python 2.6,Django 1.6和Celery 3.1,结果是后端AMQP。

In the project, I try to poll task.state of a long running task and update its running status. It worked in the development, but it won't work when I move the project on production server. I kept getting 'PENDING' even I can see the task started on flower. However, I can still get the results updated when the task finished, which when task.state == 'SUCCESS'. I use python 2.6, Django 1.6 and Celery 3.1 in the production, result backend AMQP.

@csrf_exempt
def poll_state(request):
    data = 'Fail'

    if request.is_ajax():
            if 'task_id' in request.POST.keys() and request.POST['task_id']:
                    task_id = request.POST['task_id']
                    email = request.POST['email']
                    task = AsyncResult(task_id)
                    print "task.state=", task.state
                    if task.state == 'STARTED':
                            task_state = 'Running'
                            data = 'Running'
                            #data = 'Running'
                    elif task.state == 'PENDING' or task.state == 'RETRY':
                            task_state = 'Waiting'
                            data = 'Pending'
                    elif task.state == 'SUCCESS':
                            task_state = 'Finished'
                            if task.result:
                                    data = task.result
                            else:
                                    data = 'None'

                    else:
                            task_state = task.state
                            data = 'Error'
                            print 'data status =', task_state
            else:
                    task_state = task.state
                    data = 'Error'
    else:
            task_state = task.state
            data = "Error"

    json_data = json.dumps({'task_state':task_state, 'task_data':data})

 return HttpResponse(json_data, mimetype='application/json')

在另一条记录上,花总是离线显示工人的状态,但任务状态正确。使用芹菜事件3.1.12(Cipater)时,它显示正确的工作人员状态。

on another note, flower always show the workers' status offline, but tasks status were correct. When using celery events 3.1.12 (Cipater), it shows correct worker status.

推荐答案

可能与 CELERY_TRACK_STARTED 设置。引用文档:

It's probably related to CELERY_TRACK_STARTED setting. Quoting the docs:


CELERY_TRACK_STARTED

CELERY_TRACK_STARTED

如果为True,任务将报告其状态如
由工作人员执行任务时开始。默认值为
False,因为正常行为是不报告该级别的
粒度。任务正在等待,完成或正在等待
重试。当有
个长期运行的任务并且需要报告当前正在运行哪个任务
时,具有开始状态非常有用。

If True the task will report its status as "started" when the task is executed by a worker. The default value is False as the normal behaviour is to not report that level of granularity. Tasks are either pending, finished, or waiting to be retried. Having a "started" state can be useful for when there are long running tasks and there is a need to report which task is currently running.

也许您的开发设置中有 CELERY_TRACK_STARTED = True ,但在生产环境中没有?

Maybe you have CELERY_TRACK_STARTED = True in your development settings, but not in production ?

这篇关于AsyncResult(task_id)返回“ PENDING”在任务开始后也要声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆