This is probably a stupid question but its got me stumped coming from a Ruby background.
I have an object that looks like this when I try to print it.
print celery.AsyncResult.task_id
>>><property object at 0x10c383838>
I was expecting the actual value of the task_id property to be printed here. How do I get to the actual value?
UPDATE 1
@celery.task
def scan(host):
print celery.AsyncResult.task_id
cmd = 'ps -ef'
cm = shlex.split(cmd)
scan = subprocess.check_output(cm)
return scan
Best Regards.
Short story, within function
scan
, usescan.request.id
.See http://docs.celeryproject.org/en/latest/userguide/tasks.html?highlight=request#task-request-info
In order to make your tasks more "OO-like", you could use the
bind
argument to get a reference toself
:Please note that
self.request.id
is actually an instance ofAsyncTask
. In order to have the task id as a string, you should doself.request.id.__str__()
.From Celery's documentation (after the example):
You are accessing the
property
from the class, whiletask_id
is a property of instances ofAsynchResult
.To obtain the value of
task_id
you first have to create an instance of that class, afterwards accessingasynch_result_instance.task_id
will return you the real id.In your updated code:
Here you are accessing the class as I've already explained. What you want is an instance of the currently executing task. You might use
celery.current_task
to get the currently executing task-object:Or, if you are interested in the unique id use the
request
attribute of the decorated function:In this second case do not use any local variable called
scan
otherwise you'll anUnboundLocalError
.(Code not tested since I don't have
celery
installed.)The
property
s are descriptors used to provide attribute-like access to getter/setter methods, so that you can access data like:But when the code is executed the setter or getter can control what's going on.
You can verify this with a dummy class: