Gill Bates
Gill Bates

Reputation: 15107

How to retrieve pending and executing Celery tasks with their arguments?

In Celery docs, there is the example of inspecting executing tasks:

You can get a list of active tasks using active():

>>> i.active()
[{'worker1.example.com':
    [{'name': 'tasks.sleeptask',
      'id': '32666e9b-809c-41fa-8e93-5ae0c80afbbf',
      'args': '(8,)',
      'kwargs': '{}'}]}]

But this call returns only representations of arguments, obtained by repr(). Is there way to get serialized tasks arguments?

Upvotes: 9

Views: 899

Answers (2)

economy
economy

Reputation: 4251

OK, I'm gonna drop this in as an answer. Hope this addresses your concern.

Celery offers up a string for the args. To handle it, and get a list:

args = '(5,6,7,8)' # from celery status

as_list = list(eval(args))

Of course, eval() is a little dangerous, so you may want to use literal eval:

import ast

args = '(5,6,7,8)' # from celery status

as_list = list(ast.literal_eval(args))

That's how I handle parsing Celery args in my workflows. It's kind of a pain.

Upvotes: 7

craigts
craigts

Reputation: 3067

How willing to hack on the core Celery code are you? The representation returned via .active() ultimately comes through this code: https://github.com/celery/celery/blob/b1deab39aad2fdec95f48b9f6e19ca1967285544/celery/utils/saferepr.py#L68

And is set on the request here: https://github.com/celery/celery/blob/master/celery/worker/request.py#L120

You could modify those functions to return whatever representation of objects you desired... of course doing so might break something else.

Also, @economy had a good comment about possibly eval'ing the repr. All depends on what your main goal is.

Upvotes: 5

Related Questions