#SORA#celery原生配置文件研究

ps:百度是xxx的走狗

回到正题,今天研究了下用一个py文件作为celery的配置文件,所以,还是参考昨天的例子:http://my.oschina.net/hochikong/blog/396079

我们把celery.py的配置项拿出来,在proj目录中创建celeryconfig.py,内容如下:

CELERY_TASK_RESULT_EXPIRES=3600
CELERY_TASK_SERIALIZER=‘json‘
CELERY_ACCEPT_CONTENT=[‘json‘]
CELERY_RESULT_SERIALIZER=‘json‘

修改celery.py:

from __future__ import absolute_import
from celery import Celery
app = Celery(‘proj‘,
             broker=‘amqp://[email protected]//‘,
             backend=‘amqp://[email protected]//‘,
             include=[‘proj.agent‘])
#app.conf.update(
#    CELERY_TASK_RESULT_EXPIRES=3600,
#    CELERY_TASK_SERIALIZER=‘json‘,
#    CELERY_ACCEPT_CONTENT=[‘json‘], 
#    CELERY_RESULT_SERIALIZER=‘json‘
#)
app.config_from_object(‘proj.celeryconfig‘)
if __name__ == ‘__main__‘:
  app.start()

使用app.config_from_object()导入配置,注意需要以如下格式才能导入:path:module,celeryconfig文件要和celery文件在同一目录

启动一下:

[email protected]:~/celeryapp/configtest# celery -A proj worker -l info
/usr/local/lib/python2.7/dist-packages/celery/platforms.py:766: RuntimeWarning: You are running the worker with superuser privileges, which is
absolutely not recommended!

Please specify a different user using the -u option.

User information: uid=0 euid=0 gid=0 egid=0

  uid=uid, euid=euid, gid=gid, egid=egid,
 
 -------------- [email protected] v3.1.17 (Cipater)
---- **** ----- 
--- * ***  * -- Linux-3.13.0-24-generic-x86_64-with-Ubuntu-14.04-trusty
-- * - **** --- 
- ** ---------- [config]
- ** ---------- .> app:         proj:0x7fade8bc1550
- ** ---------- .> transport:   amqp://guest:**@localhost:5672//
- ** ---------- .> results:     amqp://[email protected]//
- *** --- * --- .> concurrency: 1 (prefork)
-- ******* ---- 
--- ***** ----- [queues]
 -------------- .> celery           exchange=celery(direct) key=celery
                

[tasks]
  . proj.agent.add
  . proj.agent.mul

[2015-04-05 16:26:11,577: INFO/MainProcess] Connected to amqp://guest:**@127.0.0.1:5672//
[2015-04-05 16:26:11,603: INFO/MainProcess] mingle: searching for neighbors
[2015-04-05 16:26:12,622: INFO/MainProcess] mingle: all alone
[2015-04-05 16:26:12,648: WARNING/MainProcess] [email protected] ready.
^C
worker: Hitting Ctrl+C again will terminate all running tasks!

worker: Warm shutdown (MainProcess)

启动成功了。

途中的碰壁经历:因为我一开始是直接把celery.py的配置内容复制过来,没有去掉配置内容间的逗号,像这样:

CELERY_TASK_RESULT_EXPIRES=3600,
CELERY_TASK_SERIALIZER=‘json‘,
CELERY_ACCEPT_CONTENT=[‘json‘], 
CELERY_RESULT_SERIALIZER=‘json‘

导致无法启动worker。后来看了看官方文档的example,才搞定了这一问题。只要去掉那些逗号即可。

反思:虽然celery还可以使用这种形式的配置:

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‘)

还可以使用app.config_from_envvar()来配置,但是我觉得我用的那种方式更加方便。而且把配置内容都放在一个单独文件中,避免修改源码。

SORA可能会使用app.conf.update的方式配合configparser对worker进行配置,一方面能避免用户直接和celery配置打交道,同时还能统一整个项目的配置方式(参考openstack的各种conf文件)

时间: 2024-08-28 03:20:36

#SORA#celery原生配置文件研究的相关文章

Mybatis导入原生配置文件

在Spring-Mybatis中导入Mybatis原生配置文件 在sqlSessionFactory Bean中设置设置configLocation属性 <property name="configLocation" value="classpath:mybatis.xml"></property> 在mybatis.xml中添加配置 <?xml version="1.0" encoding="UTF-8&

#SORA#celery研究中的一个小问题

sora的rpc机制打算使用celery处理,celery+rabbitmq.最近开始研究它的文档,试着写了段代码; from celery import Celery app = Celery('cagent',backend='redis://localhost',broker='amqp://[email protected]//') #app.conf.update( #    CELERY_TASK_SERIALIZER='json', #    CELERY_ACCEPT_CONTE

#SORA#celery实践1

这次研究celery的Next Step部分. 先创建一个python module: mkdir proj cd proj touch __init__.py 在proj目录中创建celery.py: from __future__ import absolute_import from celery import Celery app = Celery('proj',              broker='amqp://',              backend='amqp://',

#sora#celery worker guide abstract

celery worker guide abstract 启动worker: e.g. celery -A proj worker -l info celery -A proj worker --loglevel=INFO --concurrency=10 -n worker1.%h 备注: The hostname argument can expand the following variables: %h: Hostname including domain name. %n: Hostn

#SORA#celery研究笔记

最近看到celery文档task部分,做一下小结 实际处理时,我们可以使用一个类似于logging的模块生成日志. 对于某些任务,你可以设置当满足某些条件时,重试任务.拒绝任务或忽略任务 在定义task时,@app.task(bind=True)中的bind参数可以让你在task中访问请求中的内容,比如id,group之类的信息 @app.task(AGRS=VALUE),ARGS有好几个参数可以设置,比如name,有些和全局设置(CELERY_xxx_xxx之类的)是一样的配置内容 可以自定义

#sora#celery笔记——call the task

基本的两种task调用方式: apply_async()和delay(),前者要把参数放在元组或字典中,后者直接使用参数 快速参考: T.delay(arg, kwarg=value) always a shortcut to .apply_async. T.apply_async((arg, ), {'kwarg': value}) T.apply_async(countdown=10) executes 10 seconds from now. T.apply_async(eta=now +

基础入门_Python-模块和包.深入Celery之应用配置/独立模块配置实践?

配置简介: 说明: Celery的配置文件非常强大,支持在应用上设置,也可以使用一个独立的配置模块,具体需要调整的默认选项可通过http://docs.jinkan.org/docs/celery/configuration.html#configuration 获取. # 方式一 : 直接在应用上设置,通过app.conf.update可一次性设置多个选项,常用于小型项目 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date    : 20

Celery,Tornado,Supervisor构建和谐的分布式系统

Celery 分布式的任务队列 与rabbitmq消息队列的区别与联系: rabbitmq 调度的是消息,而Celery调度的是任务. Celery调度任务时,需要传递参数信息,传输载体可以选择rabbitmq. 利用rabbitmq的持久化和ack特性,Celery可以保证任务的可靠性. 优点: 轻松构建分布式的Service Provider. 高可扩展性,增加worker也就是增加了队列的consumer. 可靠性,利用消息队列的durable和ack,可以尽可能降低消息丢失的概率,当wo

celery学习笔记2

1.定义: Celery是一个异步的任务队列(也叫做分布式任务队列) 2.工作结构 Celery分为3个部分 (1)worker部分负责任务的处理,即工作进程(我的理解工作进程就是你写的python代码,当然还包括python调用系统工具功能) (2)broker部分负责任务消息的分发以及任务结果的存储,这部分任务主要由中间数据存储系统完成,比如消息队列服务器RabbitMQ.redis.Amazon SQS.MongoDB.IronMQ等或者关系型数据库,使用关系型数据库依赖sqlalchem