Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

celery

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?

like image 659
Gill Bates Avatar asked Nov 24 '15 15:11

Gill Bates


2 Answers

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.

like image 107
economy Avatar answered Oct 13 '22 18:10

economy


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.

like image 38
craigts Avatar answered Oct 13 '22 19:10

craigts