Search code examples
pythondjangocelerydjango-celery

Django and Celery tasks


So in our Django project we're using Celery and the Django-Celery module. The person that originally wrote the tasks section wrote it like so:

from djcelery import celery

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

But everywhere in the docs it shows that we should create a separate celery.py file and import the app like so:

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():
    ...

So I'm wondering if there's a problem doing it one way or the other? We're using django-celery version 3.1


Solution

  • First page in 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.

    Also first line of readme for django-celery states following

    Old django celery integration project.

    To summarize following, django-celery is old approach used by your app and new documentation follows new way of handling celery