【问题标题】:Embedding celery decorator inside a class在类中嵌入 celery 装饰器
【发布时间】:2021-03-02 17:27:41
【问题描述】:

我很难给班上的芹菜装饰师打电话。我的问题是我的装饰器将“自我”视为一个论点。有什么方法可以在我的课堂上调用这个装饰器吗? 我会很感激任何帮助,谢谢!

import numpy as np
from celery import Celery

app = Celery('tasks', broker="amqp://xx@localhost")

class arithmaticFnc():
    def __init__(self):
        self.seed = 8

    @app.task
    def add_num(self, x, y):
        return x + y 
        
-----------------

A = arithmaticFnc()
result = A.add_num.delay(2, 1)

result.get()

【问题讨论】:

    标签: python celery decorator django-celery


    【解决方案1】:

    使用这样的基于类的任务:

    import numpy as np
    from celery import Celery
    
    app = Celery('tasks', broker="amqp://xx@localhost")
    
    class ArithmeticTask(app.Task):
        def __init__(self):
            self.seed = 8
    
        def run(self, x, y, *args, **kwargs):
            return x + y 
    
    app.register_task(ArithmeticTask())
            
    -----------------
    
    a = ArithmeticTask()
    result = a.delay(2, 1)
    
    result.get()
    

    【讨论】:

    • 这样尝试,python celery 不会处理该任务,因为它是“未注册且类型为无”。会不会是与旧版 celery 不兼容?
    • 除非上面的代码不在您的工作人员中,否则不确定如何注册。能否请您确认工人有上面的代码?
    猜你喜欢
    • 2013-10-24
    • 2019-09-01
    • 1970-01-01
    • 2021-12-05
    • 2014-01-14
    • 2019-08-01
    • 2012-09-28
    • 2021-11-05
    • 2020-01-11
    相关资源
    最近更新 更多