Search code examples
djangopython-3.xcelerydjango-1.9

Troubles to set up a simple task


I'm trying to set up a simple task (printing "foo" every 5 seconds) with django and celery.

Assuming I have the following project :

.
├── manage.py
└── proj
    ├── __init__.py
    ├── settings.py
    ├── tasks.py
    ├── urls.py
    └── wsgi.py

with tasks.py :

from celery import Celery
import os

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')

from django.conf import settings

app = Celery('tasks', broker='django://127.0.0.1:8000//')

app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

@app.task
def test():
    print("FOO")

And settings.py :

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'celery',
    'kombu.transport.django',
]

[...]

# Celery :                                                                                                 

CELERYBEAT_SCHEDULE = {
    'test-celery': {
    'task' : 'proj.tasks.test',
        'schedule' : timedelta(seconds=5)
    },
}

CELERY_TIMEZONE = 'Europe/Paris'

and proj/__init__.py:

from .tasks import app as celery_app

I migrate, launch with celery -A proj.tasks worker --loglevel=info and celery is launched, I just have two warning with the pickle use with this version (3.1.23) and about settings.DEBUG being True. But celery seems working. I also launch my dev server with manage.py.

So why isn't my task (test()) running?


Solution

  • You need to run Celery Beat. For example like this:

    celery -A proj worker -l info -B