Find out whether celery task exists Find out whether celery task exists python python

Find out whether celery task exists


Celery does not write a state when the task is sent, this is partly an optimization (see the documentation).

If you really need it, it's simple to add:

from celery import current_app# `after_task_publish` is available in celery 3.1+# for older versions use the deprecated `task_sent` signalfrom celery.signals import after_task_publish# when using celery versions older than 4.0, use body instead of headers@after_task_publish.connectdef update_sent_state(sender=None, headers=None, **kwargs):    # the task may not exist if sent using `send_task` which    # sends tasks by name, so fall back to the default result backend    # if that is the case.    task = current_app.tasks.get(sender)    backend = task.backend if task else current_app.backend     backend.store_result(headers['id'], None, "SENT")

Then you can test for the PENDING state to detect that a task has not (seemingly)been sent:

>>> result.state != "PENDING"


AsyncResult.state returns PENDING in case of unknown task ids.

PENDING

Task is waiting for execution or unknown. Any task id that is not known is implied to be in the pending state.

http://docs.celeryproject.org/en/latest/userguide/tasks.html#pending

You can provide custom task ids if you need to distinguish unknown ids from existing ones:

>>> from tasks import add>>> from celery.utils import uuid>>> r = add.apply_async(args=[1, 2], task_id="celery-task-id-"+uuid())>>> id = r.task_id>>> id'celery-task-id-b774c3f9-5280-4ebe-a770-14a6977090cd'>>> if not "blubb".startswith("celery-task-id-"): print "Unknown task id"... Unknown task id>>> if not id.startswith("celery-task-id-"): print "Unknown task id"... 


Right now I'm using following scheme:

  1. Get task id.
  2. Set to memcache key like 'task_%s' % task.id message 'Started'.
  3. Pass task id to client.
  4. Now from client I can monitor task status(set from task messages to memcache).
  5. From task on ready - set to memcache key message 'Ready'.
  6. From client on task ready - start special task that will delete key from memcache and do necessary cleaning actions.