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

ps:百度是xxx的走狗python


回到正題,今天研究了下用一個py文件做爲celery的配置文件,因此,仍是參考昨天的例子:http://my.oschina.net/hochikong/blog/396079 json

咱們把celery.py的配置項拿出來,在proj目錄中建立celeryconfig.py,內容以下:app

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

修改celery.py:python2.7

from __future__ import absolute_import
from celery import Celery
app = 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文件在同一目錄ui


啓動一下:.net

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 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,
 
 -------------- 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.
^C
worker: Hitting Ctrl+C again will terminate all running tasks!

worker: Warm shutdown (MainProcess)

啓動成功了。code


途中的碰壁經歷:由於我一開始是直接把celery.py的配置內容複製過來,沒有去掉配置內容間的逗號,像這樣:orm

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

致使沒法啓動worker。後來看了看官方文檔的example,才搞定了這一問題。只要去掉那些逗號便可。blog


反思:雖然celery還能夠使用這種形式的配置:ip

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文件)

相關文章
相關標籤/搜索