Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

celery periodic tasks not executing

I am learning celery and I created a project to test my configuration. I installed celery==4.0.0 and django-celery-beat==1.0.1 according to the latest documentation.

In drf_project(main project dir with manage.py)/drf_project/celery.py

from __future__ import absolute_import, unicode_literals
from celery import Celery
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'drf_project.settings')
app = Celery('drf_project')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

In drf_project/drf_project/settings.py

INSTALLED_APPS += ('django_celery_beat',)
CELERYBEAT_SCHEDULE = {
    "test_1": {
        "task": "tasks.print_test",
        "schedule": timedelta(seconds=2),
    },
}

In drf_project/drf_project/init.py

from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app

__all__ = ['celery_app']

In my user_management app (drf_project/user_mangement/) I added a tasks.py

from celery import Celery
from time import strftime

app = Celery()

@app.task
def print_test():
    print strftime('%Y-%m-%d %H:%M:%S')
    with open('abc.txt', 'ab+') as test_file:
        test_file.writeline(strftime('%Y-%m-%d %H:%M:%S'))

when i run the celery worker and my django project dev server in different terminals by:

 celery -A drf_project worker -l info

and

 python manage.py runserver

I can see my task in celery log like:

[tasks]
   . user_management.tasks.print_test

But it is not executing. Also I am not getting any error. SO what I am doing wrong? I followed the official documentation of celery.

like image 752
Manish Gupta Avatar asked Nov 28 '16 07:11

Manish Gupta


People also ask

Are celery tasks asynchronous?

Once you integrate Celery into your app, you can send time-intensive tasks to Celery's task queue. That way, your web app can continue to respond quickly to users while Celery completes expensive operations asynchronously in the background.

How does Celery task queue work?

Celery communicates via messages, usually using a broker to mediate between clients and workers. To initiate a task, the Celery client adds a message to the queue, and the broker then delivers that message to a worker. The most commonly used brokers are Redis and RabbitMQ.

How do you run Celery beat in Windows?

Celery beat doesn't execute any tasks, it only queues them when appropriate. open a terminal and run celery worker -A tasks -l info which starts a worker instance and starts cosuming the tasks you have just queued.

How does Celery retry work?

In the above example, the task will retry after a 5 second delay (via countdown ) and it allows for a maximum of 7 retry attempts (via max_retries ). Celery will stop retrying after 7 failed attempts and raise an exception.


1 Answers

For running periodic tasks you have to run two services: celery beat together with celery worker.

You can find more information at the bottom of following page.

like image 186
Mateusz Soltysik Avatar answered Oct 05 '22 02:10

Mateusz Soltysik