Search code examples
celerytaskpython-3.6

Can you get a celery task's id from a signature?


I have to implement a check to see if my celery tasks have already been created, which is why I'm trying to inspect the rabbitmq queues and looking for the task ids every time I call .delay() or apply_async(). The problem is there are a lot of signatures being passed around in my code, and I don't know how to get the task id from a signature. According to the documentation, the signature object's contents look like this:

{'task': 'tasks.add', args=(2, 2), kwargs={}, options={}}

(https://docs.celeryproject.org/en/stable/reference/celery.html)

But here the task is just a string and I'm not sure if the id has even been assigned yet. This is what I would like to do in my code:

some_task_signature= task.s(parameters)
    task_id = some_task_signature.task_id # I'm not sure if this is actually in the signature
    if not is_task_active_or_registered(app, task_id)
    some_task_signature.delay()

I'm using celery version 3.1.25 and python 3.6 deployed on a windows 10 virtual machine.

Any help would be much appreciated.


Solution

  • You can set your own id.

    task_id = uuid4()
    some_task_signature = task.s(parameters).set(task_id=str(task_id))
    some_task_signature.delay()
    AsyncResult(task_id) # get task info later
    

    There is more information here: https://github.com/celery/celery/issues/1813