首页 > 解决方案 > 我应该把 Celery 配置文件放在哪里?

问题描述

celery 文档提供了一个配置文件的示例,但不太清楚该文件的放置位置,或者如何确保工作人员在启动时读取该配置文件。

celery.py 定义了一个celery.Celery名为的对象,app我用它来装饰我的所有任务。所有任务都在/myproj/tasks/.

我的目录结构有点像这样:

/myproj/celery.py
       /celeryconfig.py # my settings
       /tasks/ # tasks go here

有什么我可以放入celery.py使其加载celeryconfig.py文件的东西,还是我需要在工作人员命令行参数上指定它?

这是我的配置文件现在的样子:

## Broker settings.
broker_url = "pyamqp://admin:ypass@mq:5672"

# List of modules to import when the Celery worker starts.
imports = ('stoneid.tasks',)

标签: pythoncelery

解决方案


阅读您的问题后,我了解到您对将配置文件保存在哪里以及如何导入这些文件感到困惑。

Celery 有很多加载配置文件的方法。其中之一是config_from_object

这将是一个简单的 python 文件,由 celery 配置组成。对于您的示例 celeryconfig.py将包括:

CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'

并将celery.py包含以下代码。

app = Celery('myproj')
default_config = 'myproj.celeryconfig'
app.config_from_object(default_config)

如果您按照上述说明进行操作,则无需通过命令行传递 conf。


推荐阅读