芹菜任务异常

Exception in celery task

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 的内联导入(完成内联导入是为了防止循环导入问题)请提供任何解决此问题的想法。

堆栈

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