celery python 对象方法
我正在尝试围绕 python 对象方法获取 celery 任务包装器。就像:
class A:
@task
def test_task(self,args):
print "BLah..test"
def main():
a= A()
args = {}
a.test_task(args)
现在失败并出现错误 test_task 需要至少 2 个参数(给定 1 个)。 我的理解是 self 对象没有被传递。为什么会这样呢?我该如何解决这个问题?
更新: 确实是我对芹菜缺乏了解。 @task 装饰器只是添加/处理 celery 任务相关参数。它不会自动将对函数的每次调用都设为 celery 任务。该函数必须被称为 a.test_task.delay(args)..其中的问题...
I am trying to get celery tasks wrapper around a python object method. Like:
class A:
@task
def test_task(self,args):
print "BLah..test"
def main():
a= A()
args = {}
a.test_task(args)
Now this fails with error test_task takes atleast 2 arguments (1 given).
My understanding is the self object is not getting passed. Why is this so? and how do i work around this?
Update:
It really was my lack of understanding of celery. the @task decorator is just to add/handle the celery task related parameters. it doesn't automatically make every call to the function a celery task. the function must be called as a.test_task.delay(args).. therein the problem...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 3.0 版开始,Celery 现在支持使用方法作为任务:
http://docs.celeryproject.org/en/latest/reference /celery.contrib.methods.html
Since version 3.0 Celery now supports using methods as tasks:
http://docs.celeryproject.org/en/latest/reference/celery.contrib.methods.html
您需要使用
test_task
作为方法吗?简单的功能可以工作吗?或者你可以使用静态方法吗?顺便说一句,您的main
函数不使用 celery 来执行test_task
,它以简单的方法运行它。Do you need to have
test_task
as method? Will simple function work? Or could you use static method? BTW, yourmain
function doesn't use celery to executetest_task
, it runs it as simple method.