Problems with the new style celery api

半城伤御伤魂 提交于 2019-12-04 17:38:30

The decorator is not used with classes, it's used for functions.

Usually you will not want to define custom task classes unless you want to implement common behavior.

So either remove the @celery.task decorator, or use it with a function.

Note that the task you define here is not bound with any celery instance

Note bound to any specific app instance:

from celery import Task

class MyTask(Task):
    pass

You can bind it later:

from celery import Celery
app = Celery(broker='amqp://')

MyTask.bind(app)

or you can use the base class available on the app:

from celery import Celery
app = Celery(broker='amqp://')

class MyTask(app.Task):
    pass

The last example is not very clean as it means you are finalizing the app at module level, this is another reason why using the task decorator with functions is a best practice, and only create custom classes to be used as a base class for decorated tasks (@task(base=MyTask)).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!