Python 为什么找不到我的 celery 配置文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4763072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 17:24:23  来源:igfitidea点击:

Why can't it find my celery config file?

pythondjangocelery

提问by TIMEX

/home/myuser/mysite-env/lib/python2.6/site-packages/celery/loaders/default.py:53: NotConfigured: No celeryconfig.py module found! Please make sure it exists and is available to Python.
NotConfigured)

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

I even defined it in my /etc/profile and also in my virtual environment's "activate". But it's not reading it.

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

回答by harry

Make sure you have celeryconfig.py in the same location you are running 'celeryd' or otherwise make sure its is available on the Python path.

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

回答by basti

I had a similar problem with my tasks module. A simple

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

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

in my package's __init__.pysolved this problem.

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

回答by fastmultiplication

you can work around this with the environment... or, use --config: it requires

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

  1. a path relative to CELERY_CHDIR from /etc/defaults/celeryd
  2. a python module name, not a filename.
  1. 来自 /etc/defaults/celeryd 的相对于 CELERY_CHDIR 的路径
  2. python 模块名称,而不是文件名。

The error message could probably use these two facts.

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

回答by Sergey Luchko

Now in Celery 4.1 you can solve that problem by that code(the easiest way):

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

import celeryconfig

from celery import Celery

app = Celery()
app.config_from_object(celeryconfig)

For Example small celeryconfig.py:

例如小celeryconfig.py

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


Also very simple way:

方法也很简单:

from celery import Celery

app = Celery('tasks')

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


Or Using a configuration class/object:

或使用配置类/对象:

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


Or how was mentioned by setting CELERY_CONFIG_MODULE

或者如何通过设置 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')


Also see:

另见: