【问题标题】:Run celery periodic task for 1 hour, it it takes more then 1 hour, expire that task?运行 celery 周期性任务 1 小时,需要超过 1 小时,该任务过期了吗?
【发布时间】:2018-10-03 16:00:05
【问题描述】:

我有一个定期的数据清除任务,每天运行一次。

我希望该任务只运行 1 小时。如果处理该任务的持续时间超过 1 小时,则使该任务到期。它会在第二天再次运行 1 小时。

我想这样做是因为如果流量很高,那么这个特定的任务会持续运行 10-15 个小时。

【问题讨论】:

    标签: python django celery celery-task celerybeat


    【解决方案1】:

    我发现最可靠的方法是首先在settings.py 中设置全局限制,其中值是int,以秒为单位:

     CELERY_TASK_SOFT_TIME_LIMIT = 3540
     CELERY_TASK_TIME_LIMIT = 3600
    

    然后,您可以捕获软时间限制异常并在硬限制命中之前清理您的任务:

    from celery.exceptions import SoftTimeLimitExceeded
    
    @shared_task
    def mytask():
        try:
            return do_work()
        except SoftTimeLimitExceeded:
            cleanup_in_a_hurry()
    

    有关详细信息,请参阅celery settings docs

    然后在定义单个任务时覆盖 soft_time_limittime_limit,例如:

    @shared_task(soft_time_limit=60, time_limit=70)
    def mytask()
        ...
    

    task docs 中有更多详细信息。

    【讨论】:

      【解决方案2】:

      您可以根据documentation设置任务时间限制

      如果您要将其设置为特定任务,您可以使用 task_time_limittask_soft_time_limit 取决于您的使用特点

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-28
        • 1970-01-01
        • 2020-08-28
        • 2021-02-05
        • 1970-01-01
        • 1970-01-01
        • 2019-05-25
        • 2018-08-06
        相关资源
        最近更新 更多