celery 文档提供了一个配置文件的例子很有帮助,但它不太清楚把这个文件放在哪里,或者如何确保工作人员在启动时读取这个配置文件。
celery.py 定义了一个celery.Celery
名为的对象,app
我用它来装饰我的所有任务。所有任务都在/myproj/tasks/
.
我的目录结构有点像这样:
/myproj/celery.py
/celeryconfig.py # my settings
/tasks/ # tasks go here
Run Code Online (Sandbox Code Playgroud)
有什么我可以放入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',)
Run Code Online (Sandbox Code Playgroud)
阅读您的问题后,我了解到您对保存配置文件的位置以及如何导入这些文件感到困惑。
Celery 有多种加载配置文件的方法。其中之一是config_from_object。
这将是一个简单的 python 文件,由 celery 配置组成。对于您的示例 celeryconfig.py
将包括:
CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
Run Code Online (Sandbox Code Playgroud)
并且celery.py
将包含下面的代码。
app = Celery('myproj')
default_config = 'myproj.celeryconfig'
app.config_from_object(default_config)
Run Code Online (Sandbox Code Playgroud)
如果您按照上述说明进行操作,则无需通过命令行传递 conf。