I have the problem, that Celery assings "Pending" status to a task for tasks that are in the queue as well as non-existing ones: See Celery documentation
I tried to solve it with a solution mentioned here: Stackoverflow but this did not work out for me.
I have the following setup:
- Celery 4.1.0
- Redis 4.0.9
- Python 3
- falcon 1.4.1
My tasks are defined the following tasks.py
:
import celery
from celery.signals import after_task_publish
CELERY_BROKER = 'redis://localhost:6379/0'
CELERY_BACKEND = 'redis://localhost:6379/0'
app = celery.Celery('my_app', broker=CELERY_BROKER, backend=CELERY_BACKEND)
@app.task(bind=True, name='testing_async')
def testing_async(self):
string_result = "This is a async task call"
return dict(status_code=1, error_message=string_result)
@after_task_publish.connect
def update_sent_state(sender=None, body=None, **kwargs):
task = app.tasks.get(sender)
backend = task.backend if task else app.backend
backend.store_result(body['id'], None, "SENT")
Then in my views.py
I call it:
from celery.result import AsyncResult
from app.tasks import testing_async
class TestAsync(object):
def on_get(self, req, resp):
task = testing_async.apply_async()
resp.status = falcon.HTTP_200
resp.body = json.dumps(dict(status=task.state, jobid=task.id))
class CheckIdentifierStatus(object):
def on_get(self, req, resp, task_id):
task_result = AsyncResult(task_id)
response = {
'status': task_result.status,
'state': task_result.state
}
resp.status = falcon.HTTP_200
resp.body = json.dumps(response)
When I run my code, my status is always "Pending", but I expected it to be "SENT".
{'jobid': '94894297-918d-41d9-824c-591a6b5ea245', 'status': 'PENDING'}
{'status': 'PENDING', 'state': 'PENDING'}
{'status': 'SUCCESS', 'state': 'SUCCESS'}
Maybe someone has a hint, where I am wrong here, that I do not see the status change here.
EDIT 1:
This issue is now in planned in the celery 5.0.0 release as of their GitHub