python - 计划任务每天在特定时间运行一次 | Celery

我想安排每天在 16h UTC 运行两个任务。

为此,我实现了这个 celery config:

from celery.schedules import crontab


CELERY_IMPORTS = ('api.tasks')
CELERY_TASK_RESULT_EXPIRES = 30
CELERY_TIMEZONE = 'UTC'

CELERYBEAT_SCHEDULE = {
    'book-task': {
        'task': 'api.tasks.get_data',
        # At 16h UTC everyday
        'schedule': crontab(hour=16),
        'args': ({'book'}),
    },
    'pencils-task': {
        'task': 'api.tasks.get_data',
        # At 16h UTC everyday
        'schedule': crontab(hour=16),
        'args': ({'pencil'}),
    }
}

在运行 celery beat -A app.celery 以启动 celery 节拍之后,我运行 celery worker -A app.celery --loglevel=info --pool=solo 以运行 celery 工作程序。

使用上面的 config ,我的两个任务从 UTC 时间 16 小时开始每分钟运行一次。我的 config 有什么问题以及如何解决?

回答1

您需要在 crontab 中指定 minute,不传递 minute 意味着 https://docs.celeryq.dev/en/stable/reference/celery.schedules.html#celery.schedules.crontab,它每分钟运行一次作业。

传递 minute=0 以在整点开始运行

from celery.schedules import crontab


CELERY_IMPORTS = ('api.tasks')
CELERY_TASK_RESULT_EXPIRES = 30
CELERY_TIMEZONE = 'UTC'

CELERYBEAT_SCHEDULE = {
    'book-task': {
        'task': 'api.tasks.get_data',
        # At 16h UTC everyday
        'schedule': crontab(minute=0, hour=16),
        'args': ({'book'}),
    },
    'pencils-task': {
        'task': 'api.tasks.get_data',
        # At 16h UTC everyday
        'schedule': crontab(minute=0, hour=16),
        'args': ({'pencil'}),
    }
}

相似文章

celery - 破折号 Celery 设置

我为我的Dash应用程序设置了docker-compose。我需要建议或首选方式来设置我的celery图像。我将celery用于以下用例,这些是可取消/可中止/已撤销的任务:上传文件模型训练创建火车,...

随机推荐

最新文章