Celery--App

1.app
Celery库必须在使用前实例化,此实例称为app。
from celery import Celery
app = Celery() #此时app的名称为其id
或者
app = Celery("tasks") #此时app的名称为tasks
 
2.app.conf
配置可以更改Celery的工作方式。选项可以直接在app实例上设置,也可以使用配置模块。
配置对象由按顺序查询的多个词典组成:
  1. 在运行时进行的更改。
  2. 配置模块(如果有)
  3. 默认配置(celery.app.defaults)。
甚至可以使用app.add_defaults() 方法添加新的默认源。
 
1.直接设置配置值:
app.conf.enable_utc = True
使用update方法一次更新多个选项:
app.conf.update( enable_utc=True, timezone='Europe/London', )
 
2.从配置模块加载示例
from celery import Celery
app = Celery()
app.config_from_object('celeryconfig')
 
从一个环境变量动态获取配置模块名示例
import os
from celery import Celery
os.environ.setdefault('CELERY_CONFIG_MODULE', 'celeryconfig')
app = Celery()
app.config_from_envvar('CELERY_CONFIG_MODULE')
然后,可以指定要通过环境使用的配置模块:
$ CELERY_CONFIG_MODULE="celeryconfig.prod" celery worker -l info
 
 

原文地址:https://www.cnblogs.com/absoluteli/p/14016970.html