本文介绍了芹菜节拍时间表:启动芹菜节拍时立即运行任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用 timedelta(days = 1)创建一个芹菜节拍时间表,则第一个任务将在24小时后执行,请引用芹菜节拍文档:

If I create a celery beat schedule, using timedelta(days=1), the first task will be carried out after 24 hours, quote celery beat documentation:

但是事实是,在很多情况下,调度程序在启动时运行任务实际上很重要,但是我没有找到一个选项,该选项可以让我在芹菜启动后立即运行任务,我不是在阅读吗?小心,还是芹菜缺少此功能?

But the fact is that in a lot of situations it's actually important that the the scheduler run the task at launch, But I didn't find an option that allows me to run the task immediately after celery starts, am I not reading carefully, or is celery missing this feature?

推荐答案

我决定可以声明每个任务的实例,并在启动celery时执行它们.我一点都不喜欢它,因为这会使开始的芹菜拍子非常慢(如果您的 PeriodicTask 慢),但是它确实可以满足我的要求.

I decide I could just declare a instance of every task, and execute them at celery launch. I don't like this at all because it makes starting celery beat extremely slow (if you have slow PeriodicTask), but it does what I want.

只需将其添加到 tasks.py 的末尾:

simply add this to the end of tasks.py:

########### spawn all tasks at launch! ############
localmess = locals().values()
for obj in localmess:
    if isclass(obj):
        if obj is not PeriodicTask and issubclass(obj, PeriodicTask):
            instance = obj()
            logger.info('running {0}'.format(obj))
            try:
                instance.run()
            except:
                logger.warn('task fail: {0}'.format(obj))
                pass

######## all tasks must be decleared above! ########

这篇关于芹菜节拍时间表:启动芹菜节拍时立即运行任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 09:26