【问题标题】:Dynamically Changing Celery Beat Settings动态改变 Celery Beat 设置
【发布时间】:2012-08-12 05:53:30
【问题描述】:

有没有办法告诉 celerybeat 在运行时更改特定任务的设置?

这个例子最好地说明了它的实用性:

我有一个定期任务,每 30 秒检查一次值。有时,基于外部触发器(我无法预测),我希望此任务将轮询频率提高到 10 秒 - 持续几分钟。

这是否可行?我知道我可以更改任务配置并重新加载 celery,但这似乎是一种混乱的做事方式......

【问题讨论】:

  • @CésarBustíos 不是真的。最终在 celery 上构建了一个内部调度程序。我认为这更像是图书馆的预期用途。

标签: python scheduled-tasks celery celerybeat


【解决方案1】:

在 schedules.py 模块中,您会发现:

class schedule(object):

    def is_due(self, last_run_at):
        """Returns tuple of two items `(is_due, next_time_to_run)`,
        where next time to run is in seconds.

        e.g.

        * `(True, 20)`, means the task should be run now, and the next
            time to run is in 20 seconds.

        * `(False, 12)`, means the task should be run in 12 seconds.

        You can override this to decide the interval at runtime,
        but keep in mind the value of :setting:`CELERYBEAT_MAX_LOOP_INTERVAL`,
        which decides the maximum number of seconds celerybeat can sleep
        between re-checking the periodic task intervals.  So if you
        dynamically change the next run at value, and the max interval is
        set to 5 minutes, it will take 5 minutes for the change to take
        effect, so you may consider lowering the value of
        :setting:`CELERYBEAT_MAX_LOOP_INTERVAL` if responsiveness is of
        importance to you.

        .. admonition:: Scheduler max interval variance

        The default max loop interval may vary for different schedulers.
        For the default scheduler the value is 5 minutes, but for e.g.
        the django-celery database scheduler the value is 5 seconds.

        """
        last_run_at = self.maybe_make_aware(last_run_at)
        rem_delta = self.remaining_estimate(last_run_at)
        rem = timedelta_seconds(rem_delta)
        if rem == 0:
            return True, self.seconds
        return False, rem

所以,你可以重写 is_due 方法来设置你自己的时间增量。

【讨论】:

  • 对此有疑问。当我返回(True,10)时,它只会在不到一秒钟的时间内重新运行任务。感觉像是日期时间问题
猜你喜欢
  • 2021-12-01
  • 2019-05-02
  • 2019-11-15
  • 2015-09-07
  • 1970-01-01
  • 2015-01-01
  • 2015-06-04
  • 2022-01-20
  • 1970-01-01
相关资源
最近更新 更多