Celery配置文件位置

时间:2017-03-03 12:24:34

标签: python celery django-celery

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实例的模块放在同一目录中?

1 个答案:

答案 0 :(得分:1)

没有必要在同一目录中配置。

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

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

或者,您可以将该目录添加到sys.path。在celery模块中,将目录附加到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中选择配置。