AsyncResult (task_id) returns the status of "PENDING" even after starting the task

In the project, I try to poll task.state a long-running task and update its execution status. He worked in development, but it will not work when I transfer the project to the production server. I continued to receive "PENDING" even if I see that the task began on a flower. However, I can still get results updated after completing the task when task.state == 'SUCCESS'. I use python 2.6, Django 1.6, and Celery 3.1 in production, resulting in 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') 

on a different note, the flower always shows the status of workers offline, but the task status is correct. When using the events of celery 3.1.12 (Cipater), it shows the correct status of the employee.

+6
source share
2 answers

This is probably due to setting CELERY_TRACK_STARTED . Quoting documents:

CELERY_TRACK_STARTED

If True, the task will report its status as "running" when the task is performed by the employee. The default value is False, since normal behavior is to not report this level of graininess. Tasks are either pending completion, completed, or pending repeated. Having an β€œinitial” state can be useful when there is and it is necessary to communicate which task is currently working.

Perhaps you have CELERY_TRACK_STARTED = True in the development settings, but not in production?

+1
source

For Celery 4.1.0 and Django 1.11.7, this is what you need in the config.py file:

Right:

 task_track_started = True 

Also fix:

 CELERY_TASK_TRACK_STARTED = True 

WRONG!:

 CELERY_TRACK_STARTED = True 

It just took me 2 hours to understand. Hope this serves someone in the near future.

0
source

Source: https://habr.com/ru/post/971620/


All Articles