当我导入模块时,Celery 不起作用。在导入之前它工作正常

Celery doesn't work when i import a module. Before importing it works fine

我的 celery 任务是更新 mongodb 数据库。我已经为它创建了一个处理程序,我正在导入到 celery.py。 Celery 似乎工作正常但是当我尝试导入处理程序模块时它会抛出错误。 django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet

celery.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 (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.

其他文件:

__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',)

settings.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

如果您查看错误堆栈跟踪,内部 OrderCount module/class 正在调用 AbstractBaseUser 模型,from django.contrib.auth.models import User / AbstractBaseUser。直到现在 django 还没有机会加载其注册表中的所有应用程序,而您正试图从注册表中找到用户模型。

您可以在 Django 应用程序中创建一个 tasks.py 文件并在那里注册您的任务。 您必须从 celery.py 文件

中删除添加芹菜任务

或者,您也可以在添加函数中本地导入 OrderCount,以避免错误。