芹菜配置文件位置

Celery Config file location

Celery 文档说配置文件应该在工作目录或 python 路径中。

from celery import Celery
from properties import config_file
import sys


sys.path.append(config_file)
app = Celery()
app.config_from_object(config_file.split('.')[0])

这里config_file是/opt/celery/celery_config.py。这个想法是让用户自由创建配置文件。文档说配置文件应该在工作目录或 sys 路径中。我在 sys 路径中添加了 config_file,但是当 worker 启动时,它抛出导入错误。

config_file 是否必须与创建 Celery 实例的模块位于同一目录中?

不需要在同一目录下配置。

简单的解决方案是在启动 celery worker 时更改目录。如果您使用 supervisor 或任何其他工具来启动 worker,您可以指定要 运行 worker 的目录。

在这种情况下,您可以将目录指定为 /opt/celery,将 celery 命令指定为 celery worker -l info --config=celery_config,这将从 /opt/celery/celery_config.py.

中选择配置

或者,您可以将该目录添加到 sys.path。在您的芹菜模块中,将目录附加到 sys.path.

import sys

from celery import Celery


sys.path.append('/opt/celery/')


app = Celery()
app.config_from_object('celery_config')

这将从 /opt/celery/celery_config.py 中选择配置。