Django 和 Celery 任务

Django and Celery tasks

所以在我们的 Django 项目中,我们使用 Celery 和 Django-Celery 模块。原来写任务部分的人是这样写的:

from djcelery import celery

@celery.task
def do_something():
    ...

但是在文档中到处都表明我们应该创建一个单独的 celery.py 文件并像这样导入应用程序:

celery.py

from celery import Celery

app = Celery('project')
if __name__=='__main__':
    app.run()

tasks.py

from celery import app # Importing `app` from our celery.py

@app.task
def do_something():
    ...

所以我想知道以这种或另一种方式进行操作是否有问题?我们使用的是 django-celery 版本 3.1

start with celery documentation

中的第一页

Previous versions of Celery required a separate library to work with Django, but since 3.1 this is no longer the case. Django is supported out of the box now so this document only contains a basic way to integrate Celery and Django. You’ll use the same API as non-Django users so you’re recommended to read the First Steps with Celery tutorial first and come back to this tutorial. When you have a working example you can continue to the Next Steps guide.

django-celery 的自述文件的第一行说明如下

Old django celery integration project.

总结如下,django-celery 是您的应用程序使用的旧方法,新文档遵循处理 celery 的新方法