尝试在 celery 任务文件中导入模型时,应用程序尚未加载
Apps aren't loaded yet when trying to import model in a celery tasks file
在任何解释之前,这是我的项目树
| projectname
|____|__init__.py
|____|celery.py
|____|settings.py
|____|urls.py
|____|wsgi.py
|app1
|app2
这是我的 celery.py
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():
''' '''
每次我尝试使用此行 from app1.models import *
将 models
导入到 celery.py
文件时,我得到:
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
并且本地服务器突然停止工作。
与类似的问题有关,但不确定这里是否如此。
我想要的是将一些模型导入到文件中,以便我可以将它们用于一些查询。
我对可能出错的地方有一点线索,但不确定。
views
从 models.py
导入内容
views
从 celery.py
导入东西,比如要执行的任务
celery.py
尝试从 models
.
导入内容
所以那个像咬自己尾巴的蛇一样的圈子对我来说很奇怪。
问题是当您尝试 在 Django 使用 <b>from app1.models import *</b>
加载配置 () 之前上传您的任务
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'projectname.settings')
app = Celery('projectname')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
当然,Celery 会检测 celery.py
文件中的任务,请记住,您已经导入了从 celery.py
到 __init__.py
的所有内容,让 Django 加载它们(Celery 东西,...)每次项目启动时。
__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 加载其配置之前导入,而 Apps 在您的 settings.py
尚未建成。
你不应该将 Django 应用程序的东西导入你的 __init__.py
文件,modules/apps 是在 Django 加载配置之前构建的(settings.py),这将引发错误 应用尚未加载 如果您尝试像 __init__.py
文件中的 models
那样上传。
根据 the documentation,Celery app.autodiscover_tasks()
能够发现 settings.INSTALLED_APPS
中任何注册良好的应用程序中的每个任务。不要在 celery.py
中导入任务,只需在您所有的应用程序中创建一个 tasks.py
文件即可。
| projectname
|____|__init__.py
|____|celery.py # contains app.autodiscover_tasks()
|____|settings.py
|____|urls.py
|____|wsgi.py
|app1
|____|tasks.py
|app2
|____|tasks.py
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
from __future__ import absolute_import
在任何解释之前,这是我的项目树
| projectname
|____|__init__.py
|____|celery.py
|____|settings.py
|____|urls.py
|____|wsgi.py
|app1
|app2
这是我的 celery.py
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():
''' '''
每次我尝试使用此行 from app1.models import *
将 models
导入到 celery.py
文件时,我得到:
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
并且本地服务器突然停止工作。
我想要的是将一些模型导入到文件中,以便我可以将它们用于一些查询。
我对可能出错的地方有一点线索,但不确定。
views
从 models.py
导入内容
views
从 celery.py
导入东西,比如要执行的任务
celery.py
尝试从 models
.
所以那个像咬自己尾巴的蛇一样的圈子对我来说很奇怪。
问题是当您尝试 在 Django 使用 <b>from app1.models import *</b>
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'projectname.settings')
app = Celery('projectname')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
当然,Celery 会检测 celery.py
文件中的任务,请记住,您已经导入了从 celery.py
到 __init__.py
的所有内容,让 Django 加载它们(Celery 东西,...)每次项目启动时。
__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 加载其配置之前导入,而 Apps 在您的 settings.py
尚未建成。
你不应该将 Django 应用程序的东西导入你的 __init__.py
文件,modules/apps 是在 Django 加载配置之前构建的(settings.py),这将引发错误 应用尚未加载 如果您尝试像 __init__.py
文件中的 models
那样上传。
根据 the documentation,Celery app.autodiscover_tasks()
能够发现 settings.INSTALLED_APPS
中任何注册良好的应用程序中的每个任务。不要在 celery.py
中导入任务,只需在您所有的应用程序中创建一个 tasks.py
文件即可。
| projectname
|____|__init__.py
|____|celery.py # contains app.autodiscover_tasks()
|____|settings.py
|____|urls.py
|____|wsgi.py
|app1
|____|tasks.py
|app2
|____|tasks.py
Tasks may work in
celery.py
file, but not when uploading Model from apps, use app.autodiscover_tasks() insteadUse as well absolute imports from the future if needed
from __future__ import absolute_import