将属性传递给基本芹菜任务的最佳实践

时间:2019-06-13 15:32:02

标签: python python-3.x celery django-celery celery-task

我只是想知道将属性传递给基类任务的最佳实践是在Celery 4.3中。

我已经有如下代码:

class BaseTask(app.Task):
    abc = None

    def __init__(self, *args, **kwargs):
        if not self.abc:
            raise NotImplementedError('Must implement the abc attribute')
        super(BaseTask, self).__init__(*args, **kwargs)

    def on_success(self, retval, task_id, args, kwargs):
        print(asd)  # and do some other work with asd

class MainTask(BaseTask):
    abc = 'Here Am I'
    def run(self, *args, **kwargs):
       print(self.abc)  # and do some other great job


MainTask = app.register_task(MainTask())

但是正如我在Celery文档中所看到的:

http://docs.celeryproject.org/en/latest/_modules/celery/app/base.html?highlight=register_task

 Note:
            This is here for compatibility with old Celery 1.0
            style task classes, you should not need to use this for
            new projects.

http://docs.celeryproject.org/en/4.0/whatsnew-4.0.html#the-task-base-class-no-longer-automatically-register-tasks

The best practice is to use custom task classes only for overriding general behavior, and then using the task decorator to realize the task

这不是我所做的最佳实践。

那么有什么更好的方法/最佳实践来将属性传递给基本任务?

0 个答案:

没有答案