【问题标题】:Module 'module' has no attribute 'celery'模块“模块”没有属性“芹菜”
【发布时间】:2021-12-24 23:04:35
【问题描述】:

我需要在我的项目中使用 Celery,但是当我尝试在我的项目中安装时遇到了一些错误。我已经阅读了Celery's doc for Django,但我一直在犯同样的错误。

我的树项目:

  • ???? 源代码
    • ???? __init__.py

    • ????芹菜.py

    • ???? 核心

      • ???? __init__.py
    • ???? project_auth

      • ???? __init__.py
      • ????序列化器.py
      • ????任务.py
    • ????设置.py

    • ???? urls.py

    • ???? wsgi.py

  • ???? texto.md

src/_init_.py:

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

src/celery.py :

import os

from celery import Celery
from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'src.settings')
app = Celery('src')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

@app.task(bind=True)
def debug_task(self):
    print(f'Request: {self.request!r}')

src/settings.py :


INSTALLED_APPS = [
    #Django apps :
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    #Meus apps : 
    'src.core',
    'src.project_auth',
    'src.product',
    #Apps de terceiros :   
    'django_celery_results',
    'rest_framework',
    "rest_framework.authtoken",    
]

CELERY_CONFIG = {
    "CELERY_TASK_SERIALIZER": "json",
    "CELERY_ACCEPT_CONTENT": ["json"],
    "CELERY_RESULT_SERIALIZER": "json",
    "CELERY_RESULT_BACKEND": None,
    "CELERY_TIMEZONE": "UTC",
    "CELERY_ENABLE_UTC": True,
    "CELERY_ENABLE_REMOTE_CONTROL": False,
}

docker-compose.yml:

version: "3.8"

services:
  
  web:    
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - 8000:8000
    depends_on:
      - db
    tty: true
    container_name: ${COMPOSE_PROJECT_NAME}_app

  db:
    container_name: ${COMPOSE_PROJECT_NAME}_db
    image: postgres:14
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    volumes:
      - postgres_data:/var/lib/postgresql/data/

  cache:
    container_name: ${COMPOSE_PROJECT_NAME}_celery_broker
    image: rabbitmq:3.9
    ports:
      - 5672:5672
    
volumes:
  postgres_data:

在 docker bash 上我运行 celery --app src.project_auth 并得到:

Error: Invalid value for '-A' / '--app': 
Unable to load celery application.
Module 'src.project_auth' has no attribute 'celery'

感谢您的帮助!

【问题讨论】:

    标签: python django celery


    【解决方案1】:

    您的 celery.py 文件似乎位于错误的位置,您应该从错误消息中将 celery.py 移动到 project_auth > 文件夹。

    Module 'src.project_auth' has no attribute 'celery'
    

    这表明 Celery 期待 src/project_auth/celery.py 而你有 src/celery.py

    另一种选择是将 bash 脚本更改为

    celery --app src
    

    祝你好运!

    【讨论】:

    • 谢谢,真的很有帮助。但是当我运行代码``` celery -A src worker -l INFO ``` 我得到了这个错误``` [2021-11-13 15:41:29,189: ERROR/MainProcess] 消费者:无法连接到 amqp: //guest:**@127.0.0.1:5672//: [Errno 111] 连接被拒绝。 16.00 秒后重试... (8/100) ```
    • 这里的问题是 Celery 在 127.0.0.1 (localhost) 上寻找 RabbitMQ,但是因为您使用的是 Docker Compose,所以它是 3 个不同的容器。 Docker Compose 将主机解析设置为容器的名称,因此在这种情况下,我们可以使用“缓存”作为主机名。您需要将 CELERY_BROKER_URL 设置为 amqp://user:pass@cache:5672 我不确定 RabbitMQ 中的默认用户/密码应该是什么。
    猜你喜欢
    • 2020-01-01
    • 2015-12-08
    • 2012-11-13
    • 2021-09-23
    • 2019-01-08
    • 2015-12-21
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    相关资源
    最近更新 更多