2015-10-14 66 views
1

我正在做一個應用程序在FLASK,我已經將芹菜納入它。但是,如果我想讓芹菜工人也能工作,我必須通過終端運行應用程序。 (celery -A app.celery worker)。我試圖從主run.py文件運行它,如下所示從FLASK應用程序運行芹菜工人

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'

任何指針,以我會做什麼錯誤將不勝感激。

+0

芹菜整點運行它作爲一個單獨的進程,使您的Web應用程序可以卸載計算密集的任務,所以爲什麼你想運行芹菜工人在你的web應用程序相同的過程? –

+0

基本上,命令'celery -A app.celery worker'啓動了芹菜工作人員和主應用程序。我正在尋找相當於 –

回答

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) 
+0

的Python腳本,它啓動了芹菜工作人員,但主應用程序不工作 –

+0

@ Irtza.QC,因爲腳本甚至沒有啓動主應用程序('app.run'被註釋)... –

+0

我還沒有測試我的源代碼。我只看到你兩次宣佈'應用'。你可以參考我的例子在https://github.com/thanhson1085/flask-celery-rabbitmq-generate-thumbnail知道如何使用FLASK,CELERRY,RABBITMQ –