【问题标题】:Django/Celery - AttributeError: module 'novopagemento' has no attribute 'celery'Django / Celery - AttributeError:模块'novopagemento'没有属性'celery'
【发布时间】:2019-01-23 16:57:13
【问题描述】:

当我尝试执行命令 celery -A novopagamento worker -l info 时,我收到以下错误:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/celery/app/utils.py", line 365, in find_app
    found = sym.app
AttributeError: module 'novopagamento' has no attribute 'app'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/celery", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.6/site-packages/celery/__main__.py", line 16, in main
    _main()
  File "/usr/local/lib/python3.6/site-packages/celery/bin/celery.py", line 322, in main
    cmd.execute_from_commandline(argv)
  File "/usr/local/lib/python3.6/site-packages/celery/bin/celery.py", line 496, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "/usr/local/lib/python3.6/site-packages/celery/bin/base.py", line 273, in execute_from_commandline
    argv = self.setup_app_from_commandline(argv)
  File "/usr/local/lib/python3.6/site-packages/celery/bin/base.py", line 479, in setup_app_from_commandline
    self.app = self.find_app(app)
  File "/usr/local/lib/python3.6/site-packages/celery/bin/base.py", line 501, in find_app
    return find_app(app, symbol_by_name=self.symbol_by_name)
  File "/usr/local/lib/python3.6/site-packages/celery/app/utils.py", line 370, in find_app
    found = sym.celery
AttributeError: module 'novopagemento' has no attribute 'celery'

我的项目结构:

novopagamento
├──novopagamento
|  ├──__init__.py
|  ├──settings.py
|  └──celery.py
├──api
|  └──tasks.py

我的芹菜档案:

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

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'novopagemento.settings')

app = Celery('novopagemento')

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

app.autodiscover_tasks()


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

我的任务文件:

from __future__ import absolute_import, unicode_literals

from celery import shared_task


@shared_task
def task_number_one():
    #code

我的设置文件:

import os

# Other Celery settings
from celery.schedules import crontab

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# .
# .
# .

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',  # Django REST framework
    'rest_framework.authtoken',
    'rest_framework_swagger',  # Swagger
    'celery', # Celery
    'api',
]

# .
# .
# .

# CELERY
CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Asia/Makassar'
CELERY_BEAT_SCHEDULE = {
    'task-number-one': {
        'task': 'api.tasks.task_number_one',
        'schedule': crontab(minute=0, hour='*/3,10-19')
    },
}

如果有趣的话,还有我的 docker-compose.yml:

db:
  image: postgres
worker:
  image: redis:4
web:
  build: .
  command: python novopagamento/manage.py migrate
  command: python novopagamento/manage.py runserver 0.0.0.0:8000
  stdin_open: true
  tty: true
  volumes:
    - .:/code
  ports:
    - "8000:8000"
  links:
    - db
    - worker

我按照本文中描述的步骤进行操作:

Celery 4 Periodic Task in Django

感谢您的耐心配合。 ;)

【问题讨论】:

  • 如果你将 celery 文件中的 app 更改为 celery,它会起作用吗?也许您复制/粘贴了一些寻找celery 变量而不是app 变量的东西?如果这有帮助,也许similar question here.
  • 我已经更改了变量名,我已经在 tasks.py 中实例化了 Celery,但没有任何效果。
  • 我以前从未在 INSTALLED_APPS 中看到过 'celery' 设置,但也许这是可能的。在我的工作应用程序中,在遵循 celery 的文档后,我在 INSTALLED_APPS 中设置了 'django_celery_results'。也许你的用例不同。
  • 这里有同样的问题。你找到解决办法了吗?

标签: python django docker celery


【解决方案1】:

远射,但在 novopagamento/novopagamento/__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',)

【讨论】:

  • 非常有用。谢谢。
【解决方案2】:

您需要在 task.py 中创建一个 celery 对象。将代理更改为您使用的代理

celery = Celery('tasks', broker='amqp://guest@localhost//')

【讨论】:

  • 也许这适用于另一种情况,但仍然,谢谢。 :)
  • @mdcg 你有没有找到解决这个问题的方法?我的工人和节拍的码头集装箱给出了同样的错误。做一些研究可能与芹菜的一些切入点有关。希望您解决了这个问题并与我分享您的发现!
  • @tomoc4 是的,我可以找到问题的解决方案,包括我在 Medium 中创建了一篇解释如何解决的帖子。不幸的是它是葡萄牙语,这是我的母语,但我请你注意“docker-compose.yml”,对于芹菜的执行有一个非常重要的细节。您可以点击此处访问帖子:link
【解决方案3】:

配置可能会导致此问题.. 替换:

CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'

收件人:

BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis'

确保 redis 服务器正在运行。

【讨论】:

  • 这是 Docker 镜像的问题。如果我在本地机器上使用它,那将是一个非常重要的细节。非常感谢。
猜你喜欢
  • 2021-12-04
  • 1970-01-01
  • 2019-06-09
  • 2018-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-04
相关资源
最近更新 更多