使用类方法作为芹菜任务 [英] using class methods as celery tasks

查看:111
本文介绍了使用类方法作为芹菜任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将class方法用作django-celery任务,并使用@task装饰器对其进行标记。 Anand Jeyahar询问此处
是这样的

I'm trying to use the methods of class as the django-celery tasks, marking it up using @task decorator. The same situation is discribed here, asked by Anand Jeyahar. It's something like this

class A:
    @task
    def foo(self, bar):
        ...

def main():
    a = A()
    ...
    # what i need
    a.foo.delay(bar) # executes as celery task 
    a.foo(bar) # executes locally

问题是,即使我使用像这样的类实例 a.foo.delay(bar)它说,就是 foo 至少需要两个参数,这意味着 self 指针会丢失。

The problem is even if i use class instance like this a.foo.delay(bar) it says, that foo needs at least two arguments, which meens that self pointer misses.

更多信息:


  • 由于继承我无法将类转换为 module

  • 方法在很大程度上依赖于类成员,所以我不能使它们成为 static

  • class 标记为带有@task装饰器的任务使该类本身成为一个任务,并且可以使用某些参数作为方法选择的键来执行 run()方法中的方法,但这不是ex

  • 创建类的实例并将其作为 self 参数传递给方法,会改变我执行方法的方式 不是芹菜,而是通常的方法(即

  • 我试图找出如何动态地注册任务的方法,例如从构造函数中进行注册,但是celery在工人之间共享代码,因此这就是为什么

  • I can't convert class to module because of inheritance
  • Methods are strongly depended on class members, so i can't make them static
  • Marking class as the task with @task decorator makes the class a task itself, and it could be possible to execute the methods from run() method, using some argument as a key for method selection, but it's not exactly what i want.
  • Creating an instance of class and passing it as self argument to methods changes the way i execute the methods not as celery taks, but as usual methods (i.e. while testing)
  • I've tried to find out how i can register the task dinamically, from constructor for example, but celery shares the code between the workers, so that's why it seems to be impossible.

感谢您的帮助!

推荐答案

Celery从3.0版开始对使用方法作为任务提供实验性支持。

Celery has experimental support for using methods as tasks since version 3.0.

此文档位于 celery.contrib.methods中。 ,还提到了一些您应该注意的警告:

The documentation for this is in celery.contrib.methods, and also mentions some caveats you should be aware of:

> https://docs.celeryproject.org/en/3.1/reference/celery.contrib.methods.html

请注意:对 contrib.methods 的支持自Celery 自4.0起

Be aware: support for contrib.methods removed from Celery since 4.0

这篇关于使用类方法作为芹菜任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆