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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With