我的芹菜任务是更新mongodb数据库。我已经为它创建了一个处理程序,并将其导入到celery.py。celery似乎工作得很好,但当我试图导入处理程序模块时,它会抛出一个错误。django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet
芹菜.py:
from __future__ import absolute_import, unicode_literals
from django.conf import settings
import os
from celery import Celery
# from handlers.ordercount import OrderCount
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'di_idealsteel.settings')
app = Celery('di_idealsteel')
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
app.config_from_object('django.conf:settings')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
@app.task(bind=True)
def add(self,number,customer_id):
ordercount = OrderCount()
opencount = ordercount.open_enquiries(number,customer_id)
当我尝试导入第5行时,它会抛出错误
Traceback(最近一次通话最后一次):文件";c: \users\ashish\envs\idealvenv\lib\site-packages\celeb\app\trace.py",第240行,在trace_task中R=retval=fun(*args,**kwargs)文件";c: \users\ashish\envs\idealvenv\lib\site-packages\celeb\app\trace.py",第437行,在__protected_call中__return self.run(*args,**kwargs)文件";C: \Users\Ashish\Desktop\Intership\di_idelsteel\di_idealsteel\cherce.py";,第32行,添加从handlers.ordercount导入ordercount文件";C: \Users\Ashish\Desktop\Intership\di_idelsteel\handlers\ordercount.py",第4行,在<模块>从handlers.user_handler导入UserHandler文件";C: \Users\Ashish\Desktop\Intership\di_idelsteel\handlers\user_handler.py",第12行,在<模块>从app.models导入(AuthUser、Customers、CustomerUsers、UserHistory、CustomerUsersHistory)文件";C: \Users\Ashish\Desktop\Intership\di_idelsteel\app\models.py",第10行,在<模块>从django.contrib.auth.models导入用户文件";c: \users\ashish\envs\idealvenv\lib\site packages\django\contrib\auth\models.py",第4行,在<模块>从django.contrib.auth.base_user导入AbstractBaseUser,BaseUserManager文件";c: \users\ashish\envs\idealvenv\lib\site-packages\django\contrib\auth\base_user.py",第49行,在<模块>类AbstractBaseUser(models.Model):文件";c: \users\ashish\envs\idealvenv\lib\site packages\django\db\models\base.py",第94行,__新__app_config=apps.get_containing_app_config(模块)文件";c: \users\ashish\envs\idealvenv\lib\site packages\django\apps\registry.py",get_containing_app_config中的第239行self.check_apps_ready()文件";c: \users\ashish\envs\idealvenv\lib\site packages\django\apps\registry.py",第124行,在check_apps_ready中提升AppRegistryNotReady("应用程序尚未加载")django.core.exceptions.AppRegistryNotReady:应用程序尚未加载
其他文件:
__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',)
设置.py:
# CELERY STUFF
BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Africa/Nairobi'
celery.py中的这一行导致了错误-->从handlers.ordercount导入ordercount
如果您查看错误stacktrace,内部OrderCount模块/类正在从django.contrib.auth.models import User/AbstractBaseUser调用AbstractBaseUser模型。到目前为止,django还没有机会加载其注册表中的所有应用程序,您正试图从注册表中找到用户模型。
你可以在django应用程序中创建一个tasks.py文件,并在那里注册你的任务。您只需要从celery.py文件中删除添加芹菜任务
或者,您也可以在add函数中本地导入OrderCount,以避免出现错误。