从FLASK app

时间:2015-10-14 05:59:23

标签: python flask celery

我在FLASK制作应用程序,我已将Celery合并到其中。但是,如果我希望Celery工作者也能工作,我必须通过终端运行应用程序。 (celery -A app.celery worker)。我尝试从主run.py文件运行它,如下所示

init.py

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.mail import Mail
from celery import Celery
from kombu import serialization


app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)
mail = Mail(app)
app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0'
app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379/0'
app.config['CELERY_ACCEPT_CONTENT'] = ['json']
app.config['CELERY_TASK_SERIALIZER'] = 'json'
app.config['CELERY_RESULT_SERIALIZER'] = 'json'
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)
serialization.registry._decoders.pop("application/x-python-serialize")

from app import views

和run.py

#!flask/bin/python
from __future__ import absolute_import, unicode_literals
from app import app
# app.run(debug=True, port=9001)

from celery import current_app    
from celery.bin import worker

app = current_app._get_current_object()

worker = worker.worker(app=app)

options = {
    'broker': app.config['CELERY_BROKER_URL'],
    'loglevel': 'INFO',
    'traceback': True,
}

worker.run(**options)

但这会产生错误AttributeError: 'Celery' object has no attribute 'config'

任何关于我做错的指示都会非常感激。

1 个答案:

答案 0 :(得分:2)

你的run.py应该是:

#!flask/bin/python
from __future__ import absolute_import, unicode_literals
from app import app
# app.run(debug=True, port=9001)

from celery import current_app    
from celery.bin import worker

application = current_app._get_current_object()

worker = worker.worker(app=application)

options = {
    'broker': app.config['CELERY_BROKER_URL'],
    'loglevel': 'INFO',
    'traceback': True,
}

worker.run(**options)