Django celery每天在特定时间运行定期任务

时间:2019-02-19 11:19:05

标签: python django celery django-celery

我想每天下午6:30运行django celery定期任务

tasks.py

import celery
from datetime import datetime
@celery.task
def my_task():
    print(' task called ')
    print(datetime.now())
    return True

celery.py

from __future__ import absolute_import, unicode_literals
from celery import Celery
from tasks import my_task
from celery.schedules import crontab

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_app.settings')
celery_instance = Celery('my_app')

celery_instance.config_from_object('django.conf:settings', namespace='CELERY')
celery_instance.autodiscover_tasks()

@celery_instance.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
    sender.add_periodic_task(
        crontab(minute=30, hour=18, day_of_week='mon,tue,wed,thu,fri,sat,sun'),
        my_task.s(),
    )

    # when i try this it work of every 1 min
    sender.add_periodic_task(
         crontab(minute='*/1'),
         my_task.s(),
   )

当我碰到芹菜时-A app_name worker -B

任务没有在下午6:30达成

0 个答案:

没有答案