Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Celery update_state() not working

I'm making progress bar but "current_task.update_state()" not working!! This is the simple example of my "task.py" definition.

from celery import shared_task, current_task

@shared_task
def mytask(a):

    list_A = [1, 2, 3]

    result = []
    for i in list_A:
        m = a * i
        result.append(m)

        process_percent = int(100 * len(result) / len(list_A))
        current_task.update_state(state='PROGRESS',
                                  meta={'process_percent':process_percent})
    return result

When I use 'process_percent' with ajax, It is always 'undefined'.
And status always return "PENDING", but return the 'result' at celery console ("celery -A myapp worker --loglevel=info --pool=solo")
So, I can see the task result.
But, I don't know why 'process_percent' is not updated....
I think "current_task.update_state" not working... And this is my celery setting.

import os
from celery import Celery
from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings')
app = Celery('myapp', broker='amqp://[email protected]:5672//', backend='amqp')

app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
like image 817
Da Woon Jung Avatar asked Feb 04 '26 01:02

Da Woon Jung


1 Answers

Probably you need a statedb: see docs for v3.1 or docs for v4

worker_state_db = '/tmp/celery_state'

As for the 'amqp' backend, please see enter link description here: "Do not use in production".

I'd recommend to have a backend like Redis or at least database for this.

like image 79
baldr Avatar answered Feb 06 '26 16:02

baldr