celery python 对象方法

发布于 2024-12-27 04:22:17 字数 437 浏览 1 评论 0原文

我正在尝试围绕 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉墨 2025-01-03 04:22:17

从 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

浸婚纱 2025-01-03 04:22:17

您需要使用 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, your main function doesn't use celery to execute test_task, it runs it as simple method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文