6

I have some Celery tasks and I'm checking their states periodically from a separate process by instantiating an AsyncResult using the task id.

From this, I get the task state. By implementing a custom results backend and extended AsyncResult class as described here, I'm able to also get the task name too. However, I want able to get a custom display name for each task - something human readable so that I can display the state info in a user friendly way.

Hypothetically, it might be set something like:

@app.task()
def my_task(args):
    display_name = "My Task"
    ...
    ...

Then later I would do...

result = ExtendedAsyncResult(task_id)
result.display_name

But from looking at the custom results backend I linked to, there doesn't appear to be any way to access the local variables of the task.

Is there a way to achieve what I'm looking for?

John B
  • 3,391
  • 5
  • 33
  • 29

1 Answers1

8

Celery support task name - hope this is what you are looking for:

@app.task(name='My Task')
def my_task(args):
    ...
    ...

the My Task will now appear wherever you want (in flower for example).

ItayB
  • 10,377
  • 9
  • 50
  • 77
  • 2
    Thanks...this probably should be the answer. I forgot to mention that I tried it, but (a) it breaks my tasks (not sure why - they just won't run anymore if I set 'name'), and (b) I wasn't sure if it's safe to use spaces etc. in names like that (even if I could get it to work). The documentation shows more variable-like names in all it's examples. It doesn't explicitly forbid anything as long as the names are unique, but I'm not confident about it. – John B Aug 20 '17 at 18:21
  • 1
    Interesting. Actually I'm using the name with underscores (and not spaces) but as far as I remember it should work with spaces as well.. it purpose is logging. Anyway, please update if u succeed, and please accept/vote if it solve your problem :-) – ItayB Aug 20 '17 at 19:21
  • 1
    I found out what was wrong, and it was my fault. There was a custom task router that ignored tasks that didn't have a certain prefix. Once I took care of that, the solution here works perfectly. – John B Aug 21 '17 at 14:23
  • Cool! Thanks for updating – ItayB Aug 21 '17 at 14:24
  • Another follow up note: the more I think about it, if I'm going to route tasks based on their names, then I can't really use this 'name' parameter for a display name, so I still might need an alternative. I don't know if routing by task name is the proper way (I'm working with someone else's code). But I've been experimenting with using a keyword argument to the task as a user friendly display name. This is easy to pass through to the result object via my custom results backend. – John B Aug 21 '17 at 14:42
  • I'm doing routing with the task name by comparing tasks names (string) and assign to properly queue, it should work for u as well – ItayB Aug 21 '17 at 14:54
  • Yeah it will work technically, it's just that the way their being routed now assumes module name spaces, since that is how the tasks are named by default. I don't want to have that limitation for my display names. – John B Aug 21 '17 at 15:27
  • 1
    What about setting this name at runtime? – CrazyGeek Sep 20 '18 at 07:25
  • @CrazyGeek don't know, let us know if you find ;-) – ItayB Sep 20 '18 at 11:12
  • Here is what I found https://celery.readthedocs.io/en/latest/userguide/tasks.html#task-names but not suffice my requirement. – CrazyGeek Sep 21 '18 at 11:29