pyprism
pyprism

Reputation: 3008

Celery periodic task not working

I tried to follow celery doc for django . Here is my project structure :

├── hiren
│   ├── celery_app.py
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
├── reminder
│   ├── admin.py
│   ├── __init__.py
│   ├── migrations
│   ├── models.py
│   ├── serializers.py
│   |── task.py
│   |── tests.py
│   |── views.py

here is my settings.py file:

BROKER_URL = 'redis://localhost:6379/4'
CELERYBEAT_SCHEDULE = {
    'run-every-5-seconds': {
        'task': 'reminder.task.run',
        'schedule': timedelta(seconds=5),
        'args': (16, 16)
    },
}

and reminder/task.py file :

def run():
    print('hello')

when I run celery -A hiren beat -l debug command I didn't see "hello" text in terminal . What I am missing ?

Upvotes: 1

Views: 2195

Answers (1)

Rahul Gupta
Rahul Gupta

Reputation: 47846

To create a task from any callable,you need to use the task() decorator. This will create a celery task for run().

reminder/task.py:

from celery import Celery

app = Celery('tasks', broker='redis://localhost')

@app.task
def run():
    print('hello')

The Celery library must be instantiated before use, this instance is called an application (or app for short).

If you are using the “old” module based celery API, then you can import the task decorator like this:

from celery import task

@task
def run():
    print('hello')

Even though this will create a celery task just like first method, but this is not recommended.

Upvotes: 5

Related Questions