django +芹菜——我如何在django应用程序中为芹菜建立crontab计划?

时间:2022-08-20 19:17:33

I am following the instructions here:

我按照这里的说明操作:

http://celery.readthedocs.org/en/latest/userguide/periodic-tasks.html#crontab-schedules

http://celery.readthedocs.org/en/latest/userguide/periodic-tasks.html#crontab-schedules

I'm supposed to be able to do the following: from celery.schedules import crontab

我应该能够做到以下几点:从celery.schedules导入crontab

In my settings.py I have:

在我的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')
    }
}

However, I'm getting an error: "No module named schedules"

但是,我收到一个错误:“没有名为scheduleles的模块”

If I switch to the other way of scheduling, using timedelta, then everything is fine and I can get my periodic tasks to run:

如果我切换到另一种调度方式,使用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)
    }
}

Why can't I use the crontab approach?

为什么我不能使用crontab方法?

1 个解决方案

#1


40  

This happens because you have a celery.py file in the same package as your settings.py, which shadows the global celery package.

发生这种情况是因为你在settings.py所在的包中有一个celery.py文件,该文件会影响全局芹菜包。

To get around this, insert the following string at the beginning of the settings.py:

要解决此问题,请在settings.py的开头插入以下字符串:

from __future__ import absolute_import

Hope it helps!

希望能帮助到你!

#1


40  

This happens because you have a celery.py file in the same package as your settings.py, which shadows the global celery package.

发生这种情况是因为你在settings.py所在的包中有一个celery.py文件,该文件会影响全局芹菜包。

To get around this, insert the following string at the beginning of the settings.py:

要解决此问题,请在settings.py的开头插入以下字符串:

from __future__ import absolute_import

Hope it helps!

希望能帮助到你!