【问题标题】:Exception in celery task芹菜任务中的异常
【发布时间】:2016-03-19 17:03:23
【问题描述】:

tasks.py

@shared_task(bind=True, default_retry_delay=60, max_retries=3)
def index_city(self, pk):
    from .models import City
    try:
        city = City.objects.get(pk=pk)
    except City.ObjectDoesNotExist:
        self.retry()
    #Do stuff here with City

当我在没有 .delay 的情况下调用上述任务时,它可以正常工作。当我在运行 celery 的开发环境中使用 .delay 调用任务时,它也可以正常工作。但是,在生产中,会抛出以下异常:

type object 'City' has no attribute 'ObjectDoesNotExist'

我添加了time.sleep(10) 以排除任何竞争条件,但这没有任何效果并且仍然抛出异常。该对象确实存在,因此似乎没有发生 City 的内联导入(进行内联导入是为了防止循环导入问题)任何解决此问题的想法都将不胜感激。

堆栈

  • Django 1.8.5
  • Python 2.7.10
  • 开发中的 sqlite 和生产中的 postgresql

【问题讨论】:

标签: django celery


【解决方案1】:

您应该使用City.DoesNotExistdjango.core.exceptions.ObjectDoesNotExist 而不是City.ObjectDoesNotExisthttps://docs.djangoproject.com/en/1.9/ref/exceptions/#objectdoesnotexist

【讨论】:

  • Thx,我不认为这会是一个问题,因为这 2 继承自 ObjectDoesNotExist,但是在按照您的建议进行更改后,我现在得到了一个适当的异常: OperationalError: FATAL: Peer authentication failed for user “克里斯”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-15
  • 1970-01-01
  • 2018-07-16
  • 2020-04-25
  • 2014-12-04
  • 2018-12-26
  • 1970-01-01
相关资源
最近更新 更多