如果卡在长时间运行的任务之后,Celerybeat会定期执行多次任务

时间:2016-04-08 02:15:32

标签: django celery python-3.4 celerybeat periodic-task

我在django程序中使用芹菜3.1.17和redis作为代理版本3.0.2。它在Ubuntu 14.04上运行。

在设置中,我将CELERYBEAT_SCHEDULE定义为:

CELERYBEAT_SCHEDULE = {
    'task1': {
        'task': 'api.tasks.exsample.task1',
        'schedule': crontab(hour=1, minute=0),
        'args': ()
    },
    'task2': {
        'task': 'api.tasks.exsample.task2',
        'schedule': crontab(hour=1, minute=30),
        'args': ()
    },
    'task3': {
        'task': 'api.tasks.exsample.task3',
        'schedule': crontab(hour=2, minute=0),
        'args': ()
    },
    'task4': {
        'task': 'api.tasks.exsample.task4',
        'schedule': crontab(hour=2, minute=30),
        'args': ()
    },
    'task5': {
        'task': 'api.tasks.exsample.task5',
        'schedule': crontab(hour=2, minute=40),
        'args': ()
    },
    'task6': {
        'task': ''api.tasks.exsample.task6',
        'schedule': crontab(hour=2, minute=50),
        'args': ()
    },
}

问题在于:

如果任务在下一个任务的计划时间之前完成,则它可以正常工作。但是如果任务运行了很长时间,比如,task1运行两个小时,那么后面的任务将分别执行几次。如果我重新开始芹菜和芹菜,有时它仍然有加班任务,但有时候没有。

让我很困惑。我一直在阅读芹菜文档,但无法弄清楚原因。任何人都可以告诉我为什么会发生这种情况,如果任务队列被阻止或芹菜重新启动,celery如何管理其消息和任务?

1 个答案:

答案 0 :(得分:0)

在settings.py中添加更多配置

'''
Task hard time limit in seconds.
The worker processing the task will be killed and replaced with a new one when this is exceeded.
'''
CELERYD_TASK_TIME_LIMIT = 86400
# CELERYD_TASK_TIME_LIMIT = 10
'''
Task soft time limit in seconds.
The SoftTimeLimitExceeded exception will be raised when this is exceeded. The task can catch this to e.g.
clean up before the hard time limit comes.
'''
CELERYD_TASK_SOFT_TIME_LIMIT = 80000
相关问题