【问题标题】:Why can't it find my celery config file?为什么找不到我的芹菜配置文件?
【发布时间】:2011-06-13 09:07:55
【问题描述】:

/home/myuser/mysite-env/lib/python2.6/site-packages/celery/loaders/default.py:53: 未配置:没有 celeryconfig.py 模块找到了!请确保它 存在并且可用于 Python。
未配置)

我什至在我的 /etc/profile 以及我的虚拟环境的“激活”中定义了它。但它不读它。

【问题讨论】:

  • 愚蠢的问题......(因为我已经这样做了)当python执行时它是否运行正确的版本。我已经在具有 2 个版本的 python 的系统上工作过......不要问。

标签: python django celery


【解决方案1】:

现在在 Celery 4.1 中,您可以通过该代码解决该问题(最简单的方法):

import celeryconfig

from celery import Celery

app = Celery()
app.config_from_object(celeryconfig)

例如小celeryconfig.py

BROKER_URL = 'pyamqp://'
CELERY_RESULT_BACKEND = 'redis://localhost'
CELERY_ROUTES = {'task_name': {'queue': 'queue_name_for_task'}}

方法也很简单:

from celery import Celery

app = Celery('tasks')

app.conf.update(
    result_expires=60,
    task_acks_late=True,
    broker_url='pyamqp://',
    result_backend='redis://localhost'
)

或使用配置类/对象:

from celery import Celery

app = Celery()

class Config:
    enable_utc = True
    timezone = 'Europe/London'

app.config_from_object(Config)
# or using the fully qualified name of the object:
#   app.config_from_object('module:Config')

或通过设置 CELERY_CONFIG_MODULE 提及的方式

import os
from celery import Celery

#: Set default configuration module name
os.environ.setdefault('CELERY_CONFIG_MODULE', 'celeryconfig')

app = Celery()
app.config_from_envvar('CELERY_CONFIG_MODULE')

另见:

【讨论】:

  • 只有文件名怎么办? /path/to/filename.py
  • @010110110101 第一个选项似乎适合您。请参阅答案,我为第一个选项添加了一些解释。如果不清楚,我愿意提供帮助。
  • 为了更容易调用config_from_object,可以通过使用关键字参数config_source直接在Celery()上设置配置来省略config_source,即app = Celery(config_source=celeryconfig)
【解决方案2】:

我的任务模块也有类似的问题。一个简单的

# celery config is in a non-standard location
import os
os.environ['CELERY_CONFIG_MODULE'] = 'mypackage.celeryconfig'

在我的包裹中__init__.py 解决了这个问题。

【讨论】:

  • 按照Celery's recommendations for Django 中的建议,在调用app = Celery('tasks') 之前,celery 配置文件中的os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings') 应该可以正常工作,尤其是如果您想稍后动态更新设置文件。
【解决方案3】:

确保 celeryconfig.py 与运行“celeryd”的位置相同,或者确保它在 Python 路径上可用。

【讨论】:

    【解决方案4】:

    您可以在环境中解决这个问题...或者,使用 --config: 它需要

    1. 来自 /etc/defaults/celeryd 的相对于 CELERY_CHDIR 的路径
    2. python 模块名称,而不是文件名。

    错误消息可能使用了这两个事实。

    【讨论】:

      猜你喜欢
      • 2016-06-24
      • 2018-03-01
      • 2018-10-07
      • 2016-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-04
      相关资源
      最近更新 更多