1.5配置

Celery使用简单,配置也非常简单。Celery有很多配置选项能够使得celery能够符合我们的需要,但是默认的几项配置已经足够应付大多数应用场景了。

  配置信息可以直接在app中设置,或者通过专有的配置模块来配置。

1.直接通过app来配置

from celery import Celery
app = Celery('demo')
# 增加配置
app.conf.update(
    result_backend='redis://:[email protected]:6379/2',
    broker_url='redis://:[email protected]:6379/1',
)

2.专有配置文件

  对于比较大的项目,我们建议配置信息作为一个单独的模块。我们可以通过调用app的函数来告诉Celery使用我们的配置模块。

  配置模块的名字我们取名为celeryconfig, 这个名字不是固定的,我们可以任意取名,建议这么做。我们必须保证配置模块能够被导入。 配置模块的名字我们取名为celeryconfig, 这个名字不是固定的,我们可以任意取名,建议这么做。我们必须保证配置模块能够被导入。

  下面我们在tasks.py模块 同级目录下创建配置模块celeryconfig.py:

result_backend = 'redis://:[email protected]:6379/2'
broker_url = 'redis://:[email protected]:6379/1'
  tasks.py文件修改为:

from celery import Celery
import celeryconfig

# 我们这里案例使用redis作为broker
app = Celery('demo')

# 从单独的配置模块中加载配置
app.config_from_object('celeryconfig')

更多配置: http://docs.celeryproject.org/en/latest/userguide/configuration.html#configuration

猜你喜欢

转载自www.cnblogs.com/alexzhang92/p/9553664.html
1.5