简体   繁体   中英

Periodic tasks not working in Django Celery

This is my periodic task:

from celery.task import PeriodicTask
from celery.registry import tasks
from datetime import timedelta
from datetime import datetime

class MyTask(PeriodicTask):
    run_every = timedelta(minutes=1)

    def run(self, **kwargs):
        self.get_logger().info("Time now: " + datetime.now())
        print("Time now: " + datetime.now())

tasks.register(MyTask)

I started my django server at python manage.py runserver and also started celery with python manage.py celeryd -B.

Still nothing gets printed on the command prompt though according to the code, the time should have been printed.

Any idea what's going wrong here? Seems quite straight forward.

You should have something like:

CELERYBEAT_SCHEDULE = {
    'proc': {
        "task": "tasks.processing",
        "schedule": timedelta(seconds=60),
        },
    }

in your celeryconfig.py or in django's settings.py See full documentaion here http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html

Check your system time-zone if is the same as Django time zone.

For Django: Change it from settings file.

For linux operating system: change it like that: ln -s /usr/share/zoneinfo/America/Chicago /etc/localtime

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM