【问题标题】:Celery doesn't work when i import a module. Before importing it works fine导入模块时芹菜不起作用。在导入之前它工作正常
【发布时间】:2020-10-11 05:45:45
【问题描述】:

我的 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(最近一次调用最后一次):
  文件“c:\users\ashish\envs\idealvenv\lib\site-packages\celery\app\trace.py”,第 240 行,在 trace_task
    R = retval = fun(*args, **kwargs)
  文件“c:\users\ashish\envs\idealvenv\lib\site-packages\celery\app\trace.py”,第 437 行,在 __protected_call__
    返回 self.run(*args, **kwargs)
  文件“C:\Users\Ashish\Desktop\Internship\di_idealsteel\di_idealsteel\celery.py”,第 32 行,添加
    从 handlers.ordercount 导入 OrderCount
   中的文件“C:\Users\Ashish\Desktop\Internship\di_idealsteel\handlers\ordercount.py”,第 4 行
    从 handlers.user_handler 导入 UserHandler
   中的文件“C:\Users\Ashish\Desktop\Internship\di_idealsteel\handlers\user_handler.py”,第 12 行
    从 app.models 导入 (AuthUser,Customers,CustomerUsers,UserHistory,CustomerUsersHistory)
   中的文件“C:\Users\Ashish\Desktop\Internship\di_idealsteel\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 行,在 __new__
    app_config = apps.get_ contains_app_config(模块)
  文件“c:\users\ashish\envs\idealvenv\lib\site-packages\django\apps\registry.py”,第 239 行,在 get_ contains_app_config
    self.check_apps_ready()
  文件“c:\users\ashish\envs\idealvenv\lib\site-packages\django\apps\registry.py”,第 124 行,在 check_apps_ready
    raise 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',)

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'

【问题讨论】:

    标签: python python-3.x django celery


    【解决方案1】:

    celery.py 中的这一行导致错误 --> 从 handlers.ordercount 导入 OrderCount

    如果您查看错误堆栈跟踪,则 OrderCount 模块/类在内部调用 AbstractBaseUser 模型,从 django.contrib.auth.models 导入用户 / AbstractBaseUser。到目前为止,django 还没有机会加载其注册表中的所有应用程序,您正试图从注册表中找到 User 模型。

    您可以在您的 django 应用程序中创建一个 tasks.py 文件并在那里注册您的任务。 您只需从 celery.py 文件中删除添加 celery 任务

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 2021-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多