0
我想在應用程序中使用芹菜異步運行長任務。瓶和芹菜大的應用程序結構
在頂部的項目文件夾我有application.py:
from flask_stormpath import StormpathManager
from app import create_app
from celery import Celery
app = create_app('DevelopmentConfig')
stormpath_manager = StormpathManager(app)
celery = Celery(app.name, broker=app.config.from_object('CELERY_BROKER_URL'))
celery.conf.update(app.config)
if __name__ == '__main__':
app.run()
的config.py看起來是這樣的:
class Config:
SECRET_KEY = 'something_very_secret'
broker_url = 'sqs://'
broker_transport_options = {'region': 'eu-west-1',
'visibility_timeout': 3600,
'polling_interval': 0.3,
'queue_name_prefix': 'celery-'}
csrf = SECRET_KEY
CELERY_BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis: //localhost:6379/0'
@staticmethod
def init_app(app):
pass
class DevelopmentConfig(Config):
JUST_SOMETHING = 'a_little_trick'
DEBUG = True
STORMPATH_API_KEY_FILE = '/.stormpath/apiKey.properties'
STORMPATH_APPLICATION = 'flask-test'
CELERY_BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis: //localhost:6379/0'
class ProductionConfig(Config):
JUST_SOMETHING = 'a_little_trick'
DEBUG = True
STORMPATH_API_KEY_FILE = '/.stormpath/apiKey.properties'
STORMPATH_APPLICATION = 'flask-test'
CELERY_BROKER_URL = 'sqs://'
config = {'development': DevelopmentConfig,
'default': DevelopmentConfig}
,並在我的views.py我嘗試運行一個任務:
from flask import render_template, flash, request, jsonify, Response
from wtforms import Form, validators, SelectMultipleField, widgets
from flask_stormpath import login_required
from . import main
import numpy as np
class MultiCheckboxField(SelectMultipleField):
widget = widgets.ListWidget(prefix_label=False)
option_widget = widgets.CheckboxInput()
@celery.task(bin=True)
def do_something(test, training_size, testing_size):
現在,當我像這樣運行時,我得到的消息是@ celery.task名稱芹菜沒有定義。公平點,所以我將它改爲@ main.celery.task。當我這樣做,我得到錯誤信息「AttributeError的:‘藍圖’對象有沒有屬性‘芹菜’
然後我試圖啓動芹菜在初始化 .py文件:
from flask import Flask
from celery import Celery
def create_app(config_name):
app = Flask(__name__)
configuration = "config."+config_name
app.config.from_object(configuration)
celery = Celery(app.name, broker=app.config.from_object('CELERY_BROKER_URL'))
celery.conf.update(app.config)
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
return app
當我這樣做,我得到的錯誤:導入錯誤:沒有模塊名爲CELERY_BROKER_URL
所以,我不知道,在導入和啓動芹菜,以及如何創建一個藍圖,這樣我可以在視圖中使用celery.task .py。任何幫助將不勝感激。
'的app.config [ 'CELERY_BROKER_URL']',你訪問它,不加載配置。 – davidism
首先,你應該把所有芹菜任務放在一個「模塊」中。 'celery = Celery(...)'應該在模塊中使用任務進行初始化,但不能在'def create_app'中進行初始化 –