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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With