In my django application I am using celery. In a post_save signal, I am updating the index in elastic search. But for some reason the task gets hung and never actually executes the code:
What I use to run celery:
celery -A collegeapp worker -l info
The Signal:
@receiver(post_save, sender=University)
def university_saved(sender, instance, created, **kwargs):
"""
University save signal
"""
print('calling celery task')
update_university_index.delay(instance.id)
print('finished')
The task:
@task(name="update_university_index", bind=True, default_retry_delay=5, max_retries=1, acks_late=True)
def update_university_index(instance_id):
print('updating university index')
The only output I get is calling celery task
. after waiting over 30 minutes, it doesn’t ever get to any other print statements and the view continue to wait. Nothing ever shows in celery terminal.
Versions:
Django 3.0,
Celery 4.3,
Redis 5.0.9,
Ubuntu 18
UPDATE:
after doing some testing, using the debug_task
defined inside the celery.py
file in place of update_university_index
does not lead to hanging. It behaves as expect. I thought maybe it could have been app.task
vs task
decorator but it seems that’s not it.
@app.task(bind=True)
def debug_task(text, second_value):
print('printing debug_task {} {}'.format(text, second_value))
2
Answers
I'm still not sure as to why it doesn't work but I found a solution by replace
task
withapp.task
importing
app
from mycelery.py
seemed to have resolved the issue.This happened with me once, I had made the dumbest error, django tells us to specify celery tasks in
tasks.py
file, and uses that for task discovery. After that it worked. Could you provide more insight into the directory structure usingtree
command?This tutorial is for
flask
, but the same can be achieved in django. Where this particular tutorial shines is that after you tell celery to execute a task, it also provides you with auuid
and you canping
that url and monitor the progress of the task you triggered.Verify that the tasks have been registered by celery using (Do make sure that celery is running):
Or bash
From https://docs.celeryproject.org/en/latest/faq.html#the-worker-isn-t-doing-anything-just-hanging
Change this line
To
Or add
self
to the task definition.