python - 我应该把 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',)
解决方案
阅读您的问题后,我了解到您对将配置文件保存在哪里以及如何导入这些文件感到困惑。
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。
推荐阅读
- python - 防止睡眠模式 python (Wakelock on python)
- reactjs - NextJS - 无效的挂钩调用。Hooks 只能在函数组件的主体内部调用
- c# - 如何使用 .net 框架安装创建 c# 应用程序设置文件?
- ios - 创建新分支不会反映 Xcode 项目中的所有目标
- android - Gradle 同步失败:找不到任务“:cdvPrintProps”的参数的leftShift()方法
- firebase - 如何从异步函数中正确获取字符串?
- javascript - 如何使用javascript替换对象中的字符和符号
- android - 如何使用 volley 读取 XML 中的变量
- java - 有多个数据源时的Spring动态事务管理器
- python - python dockerpy 令牌问题使用 jupyter notebook 运行 docker