无法让 Celery run_every 属性正常工作

发布于 2024-09-18 15:42:05 字数 1042 浏览 2 评论 0原文

我正在尝试创建一些 Celery 定期任务,其中一些需要能够在运行时更改 run_every 。 Celery 文档说我应该能够通过将 run_every 属性转换为属性来做到这一点(http://packages.python.org/celery/faq.html#can-i-change-the-interval-of-a-运行时周期性任务)。

这是我正在做的事情:

class ParseSomeStuffTask(PeriodicTask):

    def run(self, **kwargs):
        # Do stuff

    @property
    def run_every(self):
        if datetime.now().weekday() in [1, 2, 3]:
            return timedelta(minutes=15)
        else:
            return timedelta(seconds=40)

不幸的是,当我打开 celerybeat 时,出现以下错误:

[2010 年 9 月 9 日星期四 15:44:40: CRITICAL/828]:celerybeat 升高 例外 : 'datetime.timedelta' 对象没有 属性“is_due”

然后关闭。 Celery 文档并没有真正讨论将 run_every 设置为属性时要返回什么,而且我在 Google 上搜索也没有运气。 Celery 变更日志称,自版本 1.0.0 以来,它能够在运行时更改定期任务的间隔。

开发。环境:

  • Python 2.6.5
  • Django 1.2.1
  • Celery 2.0.2

I'm trying to create some Celery Periodic Tasks, and a few of them need to have the ability to change the run_every time at runtime. The Celery documentation says I should be able to do this by turning the run_every attribute into a property (http://packages.python.org/celery/faq.html#can-i-change-the-interval-of-a-periodic-task-at-runtime).

Here is what I'm doing:

class ParseSomeStuffTask(PeriodicTask):

    def run(self, **kwargs):
        # Do stuff

    @property
    def run_every(self):
        if datetime.now().weekday() in [1, 2, 3]:
            return timedelta(minutes=15)
        else:
            return timedelta(seconds=40)

Unfortunately, when I turn on celerybeat, I get the following error:

[Thu Sep 09 15:44:40 2010:
CRITICAL/828]: celerybeat raised
exception :
'datetime.timedelta' object has no
attribute 'is_due'

It then shuts down. The Celery documentation doesn't really go into what to return when making run_every a property, and I haven't had any luck searching Google. Celery changelogs say its been able to change a Periodic Task's interval at runtime since version 1.0.0.

Dev. Environment:

  • Python 2.6.5
  • Django 1.2.1
  • Celery 2.0.2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海未深 2024-09-25 15:42:05

Celery 2.0 支持不同的调度行为。有 celery.task.schedules.schedule 和 celery.task.schedules.crontab 。

您必须返回其中之一,或者创建您自己的时间表子类。

from celery.task.schedules import schedule

@property
def run_every(self):
    if datetime.now().weekday() in [1, 2, 3]:
        return schedule(timedelta(minutes=15))
    else:
        return schedule(timedelta(seconds=40))

run_every 属性将在实例化时自动转换,
但不晚了。

Celery 2.0 supports different schedule behaviors. There's celery.task.schedules.schedule and celery.task.schedules.crontab.

You have to return one of these, or make your own subclass of schedule.

from celery.task.schedules import schedule

@property
def run_every(self):
    if datetime.now().weekday() in [1, 2, 3]:
        return schedule(timedelta(minutes=15))
    else:
        return schedule(timedelta(seconds=40))

The run_every attribute will be automatically converted at instantiation,
but not later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文