博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
#SORA#celery原生配置文件研究
阅读量:6946 次
发布时间:2019-06-27

本文共 2876 字,大约阅读时间需要 9 分钟。

hot3.png

161711_33P3_987833.jpg

ps:百度是xxx的走狗

回到正题,今天研究了下用一个py文件作为celery的配置文件,所以,还是参考昨天的例子:

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

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

修改celery.py:

from __future__ import absolute_importfrom celery import Celeryapp = Celery('proj',             broker='amqp://guest@localhost//',             backend='amqp://guest@loaclhost//',             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文件在同一目录

启动一下:

root@workgroup0:~/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 isabsolutely 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,  -------------- celery@workgroup0 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://guest@loaclhost//- *** --- * --- .> 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] celery@workgroup0 ready.^Cworker: 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 Celeryapp = 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文件)

转载于:https://my.oschina.net/hochikong/blog/396291

你可能感兴趣的文章
Mysql 生成按月份统计SQL语句,为null设置为0
查看>>
驰骋工作流程引擎回答湖南朋友的21个问题
查看>>
使用htmlPurifier 过滤输入能不能不要把&转义成&
查看>>
6、OC —— 内存管理基本概念
查看>>
在多台linux主机上执行相同的命令
查看>>
1.6的锁优化(适应性自旋/锁粗化/锁削除/轻量级锁/偏向锁)
查看>>
vsm安装(6)
查看>>
webapi使用System.Web.Http.Cors配置跨域访问的几点注意事项
查看>>
使用 JIRA API 更新用户头像
查看>>
IE浏览器“增强保护模式”的笔记
查看>>
SEP防火墙规则处理顺序
查看>>
linux AMP默认安装位置
查看>>
oracle表空间的创建、删除、查看、表空间不存在、及修改默认表空间详解
查看>>
Docker-compose install
查看>>
函数的使用
查看>>
ACL的使用和查看系统上登录的用户的命令使用
查看>>
C/C++源码网站
查看>>
SICP 2.40 2.41 2.42 2.43
查看>>
建立cover组 成员有cover01 cover02 建立team组 成员有team 01 team02 建立user组 成员有user...
查看>>
linux pxe 系统自动化安装
查看>>