Django应用程序还没有加载Celery任务

时间:2016-09-24 13:10:28

标签: python django celery

下面给出错误的是什么?我不确定这是我安装的应用程序或我的应用程序的问题。以下例外仅在运行Celery时生成,即celery -A demo.apps.wall.tasks worker,runserver不会生成任何错误。问题是哪个应用程序?

Traceback (most recent call last):
  File "/Users/user/Documents/workspace/demo-api/env/bin/celery", line 11, in <module>
    sys.exit(main())
  File "/Users/user/Documents/workspace/demo-api/env/lib/python3.5/site-packages/celery/__main__.py", line 30, in main
    main()
  File "/Users/user/Documents/workspace/demo-api/env/lib/python3.5/site-packages/celery/bin/celery.py", line 81, in main
    cmd.execute_from_commandline(argv)
  File "/Users/user/Documents/workspace/demo-api/env/lib/python3.5/site-packages/celery/bin/celery.py", line 770, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "/Users/user/Documents/workspace/demo-api/env/lib/python3.5/site-packages/celery/bin/base.py", line 309, in execute_from_commandline
    argv = self.setup_app_from_commandline(argv)
  File "/Users/user/Documents/workspace/demo-api/env/lib/python3.5/site-packages/celery/bin/base.py", line 469, in setup_app_from_commandline
    self.app = self.find_app(app)
  File "/Users/user/Documents/workspace/demo-api/env/lib/python3.5/site-packages/celery/bin/base.py", line 489, in find_app
    return find_app(app, symbol_by_name=self.symbol_by_name)
  File "/Users/user/Documents/workspace/demo-api/env/lib/python3.5/site-packages/celery/app/utils.py", line 238, in find_app
    sym = imp(app)
  File "/Users/user/Documents/workspace/demo-api/env/lib/python3.5/site-packages/celery/utils/imports.py", line 101, in import_from_cwd
    return imp(module, package=package)
  File "/Users/user/Documents/workspace/demo-api/env/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 662, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/Users/user/Documents/workspace/demo-api/demo/apps/Walls/tasks.py", line 14, in <module>
    from demo.apps.Walls.redis_models import WallSchedule, WallBroadcast, UserWalls
  File "/Users/user/Documents/workspace/demo-api/demo/apps/Walls/redis_models.py", line 12, in <module>
    from demo.apps.memberships.models import Membership
  File "/Users/user/Documents/workspace/demo-api/demo/apps/memberships/models.py", line 4, in <module>
    from django.contrib.contenttypes.models import ContentType
  File "/Users/user/Documents/workspace/demo-api/env/lib/python3.5/site-packages/django/contrib/contenttypes/models.py", line 161, in <module>
    class ContentType(models.Model):
  File "/Users/user/Documents/workspace/demo-api/env/lib/python3.5/site-packages/django/db/models/base.py", line 94, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/Users/user/Documents/workspace/demo-api/env/lib/python3.5/site-packages/django/apps/registry.py", line 239, in get_containing_app_config
    self.check_apps_ready()
  File "/Users/user/Documents/workspace/demo-api/env/lib/python3.5/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.

正常运行manage.py check

5 个答案:

答案 0 :(得分:9)

尝试将其添加到tasks.py的开头:

import django
django.setup()

答案 1 :(得分:1)

好的 - 在发布了芹菜应用程序文件后,我将其与我所拥有的相比并尝试运行。 思考我发现了您的问题 - 您似乎正在调用celery -A demo.apps.wall.tasks中的tasks.py.

如果您的墙模块包含celery.py和tasks.py - 您应该致电celery -A demo.apps.wall

这是我的目录结构,以及我运行的celery命令:

django_project
- an_app
- celery_tasks
   - init.py
   - celery_app.py (celery.py in your case)
   - tasks.py

我运行的命令:来自django_project目录的celery worker -A celery_tasks

答案 2 :(得分:1)

出现AppRegistryNotReady: Apps aren't load异常的主要原因是,如果您在初始化celery应用程序之前尝试在应用程序中导入任何文件。

请勿尝试在celery.py中导入task.py或传递task.py这样的函数 app.autodiscover_tasks(task1)

最佳做法是在您的task.py

中使用@shared_task

,也将app.autodiscover_tasks()留空。 这是a link to a sample git

如果无法将模型导入到task.py,
1.检查celery.py是否清除,没有导入任何应用程序文件。
2.不要将参数传递给autodiscover_tasks()(不必要的参数)autodiscover_tasks()(自动搜索@shared_tasks

答案 3 :(得分:1)

一种可能的情况是,您在文件顶部导入模型。将这些导入移动到使用它们的方法/函数中。

答案 4 :(得分:0)

ContentType 为您提供您安装的应用程序的名称,因此如果您在模型运行之前导入它们将导致此错误,

try:
   from django.contrib.contenttypes.models import ContentType
except:
   pass
  • 应用也有同样的问题
try:
   from django.apps import apps
except:
   pass
  • 或者,您可以在使用它的函数中导入它,而不是在文件顶部导入它
    def MyFunction(self):
        from django.contrib.auth.models import Permission
        from django.contrib.contenttypes.models import ContentType
        from django import apps

        ....

  • 其他解决方案在您的文件顶部添加此文件,以告诉 Django 设置在哪里

import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'MyProject.settings')

import django
django.setup()