使用 Django-Celery 重试任务 - Django/Celery

发布于 2024-10-15 07:08:14 字数 718 浏览 1 评论 0原文

我在重试任务时遇到问题,这是测试任务的样子,

from celery.decorators import task

@task()
def add(x, y):
    if not x or not y:
        raise Exception("test error")
    return x+y

我找不到任何有关如何重试装饰任务的文档,我发现的只是:

self.retry(x,y, exc=exception, countdown=30)

这似乎不适用于我的情况该方法没有传递 self 变量。

编辑:

我现在尝试以下操作无效:

from celery.decorators import task

@task()
def add(x, y):
    if not x or not y:
        try:
            raise Exception("test error")
        except Exception, e:
            add.retry([x, y], exc=e, countdown=30)
    return x+y

我收到以下错误:

TypeError(“重试的 kwargs 参数不能为空。任务必须接受 **kwargs,请参阅

I'm having problems retrying tasks, here is what a test task looks like

from celery.decorators import task

@task()
def add(x, y):
    if not x or not y:
        raise Exception("test error")
    return x+y

I cannot find any documentation what-so-ever on how to retry decorated tasks, all I found was this:

self.retry(x,y, exc=exception, countdown=30)

which doesn't seem to work with my case as there is not self variable being passed from the method.

Edit:

I'm trying the following now to no avail:

from celery.decorators import task

@task()
def add(x, y):
    if not x or not y:
        try:
            raise Exception("test error")
        except Exception, e:
            add.retry([x, y], exc=e, countdown=30)
    return x+y

I get the following error:

TypeError("kwargs argument to retries can't be empty. Task must accept **kwargs, see http://bit.ly/cAx3Bg",)

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

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

发布评论

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

评论(2

南风几经秋 2024-10-22 07:08:14

您可以在装饰器中设置重试参数:

@task(default_retry_delay=5 * 60, max_retries=12)
def foo(bar):
  try:
      ...
  except Exception, exc:
      raise foo.retry(exc=exc)

You can set your retry parameters in the decorator:

@task(default_retry_delay=5 * 60, max_retries=12)
def foo(bar):
  try:
      ...
  except Exception, exc:
      raise foo.retry(exc=exc)
蓝梦月影 2024-10-22 07:08:14

该任务需要接受关键字参数,它们用于传递有关重试计数的信息。我认为代码应该是这样的:

from celery.decorators import task

@task()
def add(x, y, **kwargs):
    if not x or not y:
        try:
            raise Exception("test error")
        except Exception, e:
            add.retry(args=[x, y], exc=e, countdown=30, kwargs=kwargs)
    return x+y

**kwargs 需要添加到 add 函数的签名中,并作为 kwargs=kwargs 传递> 调用重试时。

注意:此样式随着 celery 2.2 的发布而被弃用

The task needs to accept keyword arguments, they are used to pass information amongst other about the retry count. I think the code should look like this:

from celery.decorators import task

@task()
def add(x, y, **kwargs):
    if not x or not y:
        try:
            raise Exception("test error")
        except Exception, e:
            add.retry(args=[x, y], exc=e, countdown=30, kwargs=kwargs)
    return x+y

**kwargs need to be added to the signature of the add function, and passed as kwargs=kwargs when calling retry.

Note: this style was deprecated with the release of celery 2.2.

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