尝试在celery任务文件中导入模型时,尚未加载应用程序 [英] Apps aren't loaded yet when trying to import model in a celery tasks file

查看:62
本文介绍了尝试在celery任务文件中导入模型时,尚未加载应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在进行任何解释之前,这是我的项目的树

Before any explanations, here is the tree of my project

| projectname
|____|__init__.py
|____|celery.py
|____|settings.py
|____|urls.py
|____|wsgi.py
|app1
|app2

这是我的芹菜.

from celery import Celery
from celery import shared_task

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'projectname.settings')
app = Celery('projectname')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

from app1.models import *

@share_task
def tasks():
     ''' '''

每次我尝试通过app1.models import * 的这一行将 models 导入到 celery.py 文件时,我得到:

Every time I try importing models to the celery.py file with this line from app1.models import * I got:

django.core.exceptions.AppRegistryNotReady:应用尚未加载.

django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

本地服务器突然停止工作.这篇文章与一个类似的问题有关,但不确定在这里是这种情况.

And the local server stops working all of a sudden. This post is related to a similar problem, but not sure it's the case here.

我想要的是将一些模型导入文件,以便可以将其用于某些查询.

我对可能出什么问题有一点线索,但不确定.

I got a little clue about what can be wrong, but not sure.

视图 models.py
导入内容 views celery.py 导入内容,例如要执行的任务
celery.py 尝试从 模型 导入内容.

views import stuff from models.py
views import stuff from celery.py like the task to be executed
celery.py tries to import stuff from models.

所以像蛇一样咬自己尾巴的圆圈对我来说很奇怪.

So that circle like a snake that bites its own tail is weird to me.

推荐答案

问题是当您尝试在Django使用

The problem is when you try to upload your tasks before Django loads the configuration() with

from app1.models import *

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'projectname.settings')
app = Celery('projectname')

app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

当然,芹菜会检测 celery.py 文件中的任务,请记住您已将所有内容从 celery.py 导入到 __ init __.py 在项目每次启动时让Django加载它们(Celery的东西,...).

Off course Celery will detect tasks in celery.py file, remember that you have imported everything from celery.py to __init__.py to let Django loads them (Celery stuff,...) every time the project starts.

__ init __.py

from __future__ import absolute_import, unicode_literals

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app

__all__ = ['celery_app']

因此,在这种情况下,您也要在该 celery.py 文件中导入模型,也要说 __ init.py __ ,您的模型将在Django加载其配置之前被导入而您的 settings.py 中的应用尚未构建.

So with this case you're importing models in that celery.py file let's say __init.py__ as well, Your models will be imported before Django loads its configuration while Apps in your settings.py aren't built yet.

您不应将Django应用程序的东西导入到您的 __ init __.py 文件中,模块/应用程序是在Django加载配置(settings.py)之前构建的,这会引发错误 Apps aren如果您尝试像 __ init __.py 文件中的 models 一样上载,则尚未加载.

You should not import Django app stuffs into your __init__.py file, modules/apps are built before Django loads the configuration(settings.py), this will raise an Error Apps aren't loaded yet if you try to upload like models in __init__.py file.

根据文档,Celery的 app.autodiscover_tasks()能够在 settings.INSTALLED_APPS 中发现任何已正确注册的应用程序中发现的每个任务.无需在 celery.py 中导入任务,只需在所有应用程序中创建一个 tasks.py 文件即可.

According to the documentation, Celery has app.autodiscover_tasks() able to discover every tasks found in any well-registered app in settings.INSTALLED_APPS. Instead of importing tasks in the celery.py Just create a tasks.py file in all your apps.

| projectname
|____|__init__.py
|____|celery.py # contains app.autodiscover_tasks()
|____|settings.py
|____|urls.py
|____|wsgi.py
|app1
|____|tasks.py
|app2
|____|tasks.py

任务可能在 celery.py 文件中工作,但从应用程序上载模型时无效,请改用app.autodiscover_tasks()

Tasks may work in celery.py file, but not when uploading Model from apps, use app.autodiscover_tasks() instead

如果需要,还可以使用将来的绝对进口

Use as well absolute imports from the future if needed

来自__future__的

from __future__ import absolute_import

这篇关于尝试在celery任务文件中导入模型时,尚未加载应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆