【问题标题】:AttributeError: module 'module' has no attribute 'celery'AttributeError:模块“模块”没有属性“芹菜”
【发布时间】:2020-01-01 13:54:41
【问题描述】:

我想在我的 Django 应用程序中使用 celery 来执行应该在后台运行的任务。我已经按照这些教程 Asynchronous Tasks With Django and Celery 和 First steps with Django Using Celery with Django

我的项目结构:

project
├──project
|  ├── settings
|     ├──__init__.py   (empty)
|     ├──base.py
|     ├──development.py
|     └──production.py
|  ├──__init__.py
|  └──celery.py
├──app_number_1
|  └──tasks.py

project/project/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']

项目/项目/celery.py:

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings.production')

app = Celery('project')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

项目/项目/设置/production.py:

INSTALLED_APPS = [
    'background_task',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.gis',
]
.
.
.
CELERY_BROKER_URL = 'mongodb://mongo:27017'
CELERY_RESULT_BACKEND = 'mongodb://mongo:27017'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'

docker-compose.yml:

version: '3'

services:
  web:
    env_file:
      - env_vars.env
    build: .
    restart: always
    command:
      - /code/runserver.sh
    ports:
      - "80:8000"
    depends_on:
      - db
      - mongo

  db:
    ...

  mongo:
    image: mongo:latest
    restart: always

运行服务器.sh:

#!/bin/bash
sleep 1
python3 /code/project/manage.py migrate --settings=project.settings.production
python3 /code/project/manage.py runserver 0.0.0.0:8000 --settings=project.settings.production & celery -A project worker -Q celery

docker-compose up --build 之后出现以下错误:

web_1    | Running migrations:
web_1    |   No migrations to apply.
mongo_1  | 2019-08-28T10:24:26.478+0000 I  NETWORK  [conn2] end connection 172.18.0.4:42252 (1 connection now open)
mongo_1  | 2019-08-28T10:24:26.479+0000 I  NETWORK  [conn1] end connection 172.18.0.4:42250 (0 connections now open)
web_1    | Error: 
web_1    | Unable to load celery application.
web_1    | Module 'project' has no attribute 'celery'

任何提示都会很棒!

谢谢

【问题讨论】:

  • 它在 docker 之外工作吗?
  • 请注意,虽然这可能不是您的问题的原因,但有两个名为“project”的嵌套目录充其量是令人困惑的。
  • 另外,在同一级别同时拥有“settings.py”模块和“settings/”子包也无济于事(子包会影响模块 IIRC,但这仍然很令人困惑)跨度>
  • 本名不是“项目”布鲁诺...
  • 可能是文件命名有问题,因为celery.py会和celery本身有命名冲突。尝试更改该文件名,看看是否有效。

标签: docker celery django-celery


【解决方案1】:

celery 模块不包含在第一个项目文件夹中。

您可以将它移到那里,也可以通过添加一个 __init__ 模块并将 celery 命令中的应用程序实例模块设置为:

celery -A project.project worker -Q celery

【讨论】:

  • 感谢它的帮助。 in celery -A command 应该包含 celery.py
猜你喜欢
  • 2012-11-13
  • 2021-12-24
  • 2015-12-08
  • 2019-01-08
  • 2021-09-23
  • 2023-04-06
  • 2018-04-14
  • 2019-02-18
  • 1970-01-01
相关资源
最近更新 更多