Python 在 Celery 任务中获取 task_id
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18872854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting task_id inside a Celery task
提问by user1513388
This is probably a stupid question but its got me stumped coming from a Ruby background.
这可能是一个愚蠢的问题,但它让我难倒来自 Ruby 背景。
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?
我期待在此处打印 task_id 属性的实际值。我如何获得实际价值?
UPDATE 1
更新 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.
此致。
采纳答案by Chris Johnson
Short story, within function scan
, use scan.request.id
.
短篇小说,在函数内scan
,使用scan.request.id
.
See http://docs.celeryproject.org/en/latest/userguide/tasks.html?highlight=request#task-request-info
请参阅http://docs.celeryproject.org/en/latest/userguide/tasks.html?highlight=request#task-request-info
回答by Bakuriu
You are accessing the property
from the class, while task_id
is a property of instancesof AsyncResult
.
您正在property
从类访问,而task_id
是 的实例的属性AsyncResult
。
To obtain the value of task_id
you first have to create an instance of that class, afterwards accessing async_result_instance.task_id
will return you the real id.
要获取task_id
您的值,您首先必须创建该类的一个实例,然后访问async_result_instance.task_id
将返回您的真实 id。
In your updated code:
在您更新的代码中:
@celery.task
def scan(host):
print celery.AsyncResult.task_id
# ...
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:
在这里,您正在访问该类,正如我已经解释的那样。您想要的是当前正在执行的任务的实例。您可能会使用celery.current_task
获取当前正在执行的任务对象:
@celery.task
def scan(host):
print celery.current_task.task_id
Or, if you are interested in the unique id use the request
attribute of the decorated function:
或者,如果您对唯一 id 感兴趣,请使用request
装饰函数的属性:
@celery.task
def scan(host):
print scan.request.id
cmd = 'ps -ef'
cm = shlex.split(cmd)
# IMPORTANT: Do *not* use "scan = ..."!
result = subprocess.check_output(cm)
return result
In this second case do notuse any local variable called scan
otherwise you'll an UnboundLocalError
.
在第二种情况下,不要使用任何名为的局部变量,scan
否则您将使用UnboundLocalError
.
(Code not tested since I don't have celery
installed.)
(代码未测试,因为我没有celery
安装。)
The property
s are descriptorsused to provide attribute-like access to getter/setter methods, so that you can access data like:
该property
s为描述用于提供属性般的访问getter / setter方法,这样就可以像访问数据:
instance.attribute
instance.attribute = value
But when the code is executed the setter or getter can control what's going on.
但是当代码被执行时,setter 或 getter 可以控制正在发生的事情。
You can verify this with a dummy class:
您可以使用虚拟类来验证这一点:
>>> class Dummy(object):
... @property
... def a(self):
... print("called the getter!")
... return 1
...
>>> Dummy.a
<property object at 0x7fdae86978e8>
>>> Dummy().a
called the getter!
1
回答by Victor
In order to make your tasks more "OO-like", you could use the bind
argument to get a reference to self
:
为了使您的任务更“类似 OO”,您可以使用bind
参数来获取对以下内容的引用self
:
@celery.task(bind=True)
def scan(self, host):
print self.request.id
Please note that self.request.id
is actually an instance of AsyncTask
. In order to have the task id as a string, you should do self.request.id.__str__()
.
请注意,这self.request.id
实际上是AsyncTask
. 为了将任务 id 作为字符串,您应该这样做self.request.id.__str__()
。
From Celery's documentation(after the example):
来自Celery 的文档(在示例之后):
The
bind
argument means that the function will be a “bound method” so that you can access attributes and methods on the task type instance.
该
bind
参数意味着该函数将是一个“绑定方法”,这样就可以在任务类型实例访问的属性和方法。