Python 芹菜不';导入模块时无法工作。在导入之前,它可以正常工作
我的任务是更新mongodb数据库。我已经为它创建了一个处理程序,并将其导入芹菜.py。芹菜似乎工作正常,但当我尝试导入处理程序模块时,它抛出了一个错误。Python 芹菜不';导入模块时无法工作。在导入之前,它可以正常工作,python,python-3.x,django,celery,Python,Python 3.x,Django,Celery,我的任务是更新mongodb数据库。我已经为它创建了一个处理程序,并将其导入芹菜.py。芹菜似乎工作正常,但当我尝试导入处理程序模块时,它抛出了一个错误。 django.core.exceptions.AppRegistryNotReady:尚未加载应用程序 芹菜。py: from __future__ import absolute_import, unicode_literals from django.conf import settings import os from celery i
django.core.exceptions.AppRegistryNotReady:尚未加载应用程序
芹菜。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)
当我尝试导入第五行时,它抛出这个错误
Traceback (most recent call last):
File "c:\users\ashish\envs\idealvenv\lib\site-packages\celery\app\trace.py", line 240, in trace_task
R = retval = fun(*args, **kwargs)
File "c:\users\ashish\envs\idealvenv\lib\site-packages\celery\app\trace.py", line 437, in __protected_call__
return self.run(*args, **kwargs)
File "C:\Users\Ashish\Desktop\Internship\di_idealsteel\di_idealsteel\celery.py", line 32, in add
from handlers.ordercount import OrderCount
File "C:\Users\Ashish\Desktop\Internship\di_idealsteel\handlers\ordercount.py", line 4, in <module>
from handlers.user_handler import UserHandler
File "C:\Users\Ashish\Desktop\Internship\di_idealsteel\handlers\user_handler.py", line 12, in <module>
from app.models import (AuthUser,Customers,CustomerUsers,UserHistory,CustomerUsersHistory)
File "C:\Users\Ashish\Desktop\Internship\di_idealsteel\app\models.py", line 10, in <module>
from django.contrib.auth.models import User
File "c:\users\ashish\envs\idealvenv\lib\site-packages\django\contrib\auth\models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "c:\users\ashish\envs\idealvenv\lib\site-packages\django\contrib\auth\base_user.py", line 49, in <module>
class AbstractBaseUser(models.Model):
File "c:\users\ashish\envs\idealvenv\lib\site-packages\django\db\models\base.py", line 94, in __new__
app_config = apps.get_containing_app_config(module)
File "c:\users\ashish\envs\idealvenv\lib\site-packages\django\apps\registry.py", line 239, in get_containing_app_config
self.check_apps_ready()
File "c:\users\ashish\envs\idealvenv\lib\site-packages\django\apps\registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
设置.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'
芹菜.py中的此行导致错误--> 从handlers.ordercount导入ordercount 如果查看错误stacktrace,OrderCount模块/类在内部从django.contrib.auth.models import User/AbstractBaseUser调用AbstractBaseUser模型。到目前为止,django还没有机会加载其注册表中的所有应用程序,您正试图从注册表中找到用户模型 您可以在django应用程序中创建tasks.py文件,并在其中注册任务。 您只需从芹菜.py文件中删除添加芹菜任务 或者,您也可以在add函数中本地导入OrderCount,以避免出现错误
# 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'