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

24 投票
4 回答
34511 浏览
提问于 2025-04-16 10:24

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

我甚至在我的/etc/profile里定义了它,还在我的虚拟环境的“activate”里也定义了。但是它就是不读取这些设置。

4 个回答

3

确保你在运行 'celeryd' 的地方有一个叫 celeryconfig.py 的文件,或者确保这个文件在 Python 的路径中可以找到。

22

我在我的任务模块中遇到了类似的问题。一个简单的

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

放在我包的 __init__.py 文件里就解决了这个问题。

39

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

另外请查看:

撰写回答