celery beat schedule: run task instantly when start celery beat?

后端 未结 2 1655
耶瑟儿~
耶瑟儿~ 2021-02-14 01:43

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:

2条回答
  •  我在风中等你
    2021-02-14 02:11

    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.

    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! ########
    

提交回复
热议问题