celery_03 配置文件configuration

简介:
celery配置文件有多种形式,用来改变celery的工做方式;能够在app实例中进行设置,也能够用单独的配置模块进行配置
 
实例:
>>> from celery import Celery
>>> app = Celery()
>>> app

 

如下几种设置方式:
 
一、经过 Celery.conf 参数设置
>>> app.conf.CELERY_TIMEZONE 'Europe/London'
 
二、直接进行设置
>>> app.conf.CELERY_ENABLE_UTC = True
 
三、经过update方法进行设置
>>> app.conf.update( ... CELERY_ENABLE_UTC=True, ... CELERY_TIMEZONE='Europe/London', ...)
 
配置文件的执行顺序是:
  1. Changes made at runtime. 在程序运行的时候修改
  2. The configuration module (if any) 调用configuration
  3. The default configuration (celery.app.defaults). 调用默认configuration
  Celery.add_defaults() 能够用来设置默认源文件
 
 
config_from_object方法介绍:
Celery.config_from_object()方法是用来加载configuration对象中的配置文件;这些configuration对象能够是configuration 模块,也能够是其余属性的配置
Celery.config_from_object()方法的优先级高,当执行改方法的时候,以前设置的配置文件会被覆盖,因此一些该方法配置以外的配置须要在该方法执行以后再进行配置
 
 
例1:用加载文件的方式导入配置文件:
定义配置文件 celeryconfig.py:
CELERY_ENABLE_UTC = True
CELERY_TIMEZONE = 'Europe/London'
导入配置文件
from celery import Celery
app = Celery()
app.config_from_object('celeryconfig')

 

例2:用配置模块的方式:
这是建议使用的配置方法,由于这样意味着这个配置文件不用在程序启动的时候进行序列化处理,会避免pickle序列化出错的状况
 
from celery import Celery
app = Celery()
import celeryconfig
app.config_from_object(celeryconfig)
 
例3:加载一个配置文件类/对象
 
from celery import Celery
app = Celery()
class Config:
    CELERY_ENABLE_UTC = True
    CELERY_TIMEZONE = 'Europe/London'
app.config_from_object(Config)
# or using the fully qualified name of the object:
#   app.config_from_object('module:Config')

 

config_from_envvar方法介绍:
Celery.config_from_envvar()从环境变量中获取配置文件的名称
例如从配置文件中加载CELERY_CONFIG_MODULE这个配置
 
import os
from celery import Celery
#: Set default configuration module name
os.environ.setdefault('CELERY_CONFIG_MODULE', 'celeryconfig')
app = Celery()
app.config_from_envvar('CELERY_CONFIG_MODULE')

 

You can then specify the configuration module to use via the environment:
$ CELERY_CONFIG_MODULE="celeryconfig.prod" celery worker -l info
(这里不太懂~~)
 
 
用字典的格式配置文件:
若是想用字典的形式去设置配置文件,就用table()方法
app.conf.table(with_defaults=False, censored=True)
 
注意的是celery并不能彻底的过滤掉敏感信息,下面的敏感词不能出如今配置文件中
API, TOKEN, KEY, SECRET, PASS, SIGNATURE, DATABASE
 
Censored configuration :设限配置操做
当打印日志将级别设置为debug或者相似的操做,一些敏感信息例如密码等不但愿被打印出来,celery提供了一些实用工具去实现这种配置,
一种方法是humanize():
app.conf.humanize(with_defaults=False, censored=True)
这种方法会返回表格格式的字符串信息,仅包含相对默认配置进行更改的配置,可是咱们能够修改with_defaults 参数
来设置是否显示默认配置
相关文章
相关标签/搜索