我按照这里的说明操作:
http://celery.readthedocs.org/en/latest/userguide/periodic-tasks.html#crontab-schedules
我应该能够做到以下几点: from celery.schedules import crontab
在我的settings.py
身上:
from kombu import serialization serialization.registry._decoders.pop("application/x-python-serialize") import djcelery djcelery.setup_loader() from celery.schedules import crontab ... CELERYBEAT_SCHEDULE = { 'first_task': { 'task': 'apps.icecream.tasks.sync_flavors', 'schedule': crontab(minute='*/30', hour='1, 3, 6, 8-20, 22') }, 'second_task': { 'task': 'apps.robots.tasks.run_robots', 'schedule': crontab(minute='*/6') } }
但是,我收到一个错误:"没有名为schedules的模块"
如果我切换到另一种调度方式,使用timedelta,那么一切都很好,我可以运行我的周期性任务:
CELERYBEAT_SCHEDULE = { 'first_task': { 'task': 'apps.icecream.tasks.sync_flavors', 'schedule': timedelta(minutes=30) }, 'second_task': { 'task': 'apps.robots.tasks.run_robots', 'schedule': timedelta(minutes=6) } }
为什么我不能使用crontab方法?
发生这种情况是因为您celery.py
在与您相同的包中有一个文件settings.py
,它会影响全局celery
包.
要解决此问题,请在settings.py的开头插入以下字符串:
from __future__ import absolute_import
希望能帮助到你!