Flask和Celery大型应用结构

时间:2017-04-25 15:44:50

标签: python flask celery

我正在尝试在应用程序中使用celery来异步运行长任务。

在顶部项目文件夹中,我有application.py:

BasicDBObject exists = new BasicDBObject("$exists", true);
BasicDBObject field = new BasicDBObject("1", exists);
BasicDBObject elemMatch = new BasicDBObject("$elemMatch", field);
BasicDBObject query = new BasicDBObject("key", elemMatch);

config.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()

并在我的views.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}

现在,当我像这样运行它时,我得到的消息是@celery.task没有定义芹菜这个名字。公平点,所以我把它改成了@ main.celery.task。当我这样做时,我收到错误消息“AttributeError:'Blueprint'对象没有属性'celery'。

然后我尝试在 init .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):

当我这样做时,我收到错误:ImportError:没有名为CELERY_BROKER_URL的模块

所以,我不知道,在哪里导入和启动芹菜以及如何创建蓝图以便我可以在views.py中使用celery.task。任何帮助都将受到高度赞赏。

0 个答案:

没有答案