如何使用celery运行SQL并从DB获取返回的结果?

时间:2016-01-10 23:11:41

标签: python flask celery flask-sqlalchemy

我正在使用Flask,Celery,Redis和Mysql来构建网站。现在我面临一个问题。我想将每个SQL查询发送到celery,一旦完成工作,我就会从celery.result获取查询结果。 这是我的代码的一部分:

Manage.py

import os
from app import create_app, db
from app.models import Users, Items
from flask.ext.script import Manager, Shell
from flask.ext.migrate import Migrate, MigrateCommand


app = create_app(os.getenv('FLASK_CONFIG') or 'default')
app.app_context().push()
manager = Manager(app)
migrate = Migrate(app, db)

def make_shell_context():
    return dict(app=app, db=db, Users=Users, Items=Items)

manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand)

if __name__ == '__main__':
    manager.run()

query.py

from .models import Items, Users
from sqlalchemy import desc
from app import create_celery_app


celery = create_celery_app()

@celery.task
def user_query(id):
    return Users.query.filter_by(id = id).first()

view.py

from ..query import user_query
...
userresult = user_query.delay(1)

main.py

from flask import Blueprint
main = Blueprint('main', __name__)
from . import views, errors

app.py

from flask import Flask
from flask.ext.bootstrap import Bootstrap
from flask.ext.moment import Moment
from config import config, Config
from celery import Celery
from flask.ext.sqlalchemy import SQLAlchemy
import os


bootstrap = Bootstrap()
moment = Moment()
db = SQLAlchemy()


def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    bootstrap.init_app(app)
    moment.init_app(app)
    db.init_app(app)

    from main import main as main_blueprint
    app.register_blueprint(main_blueprint)
    return app

def create_celery_app(app=None):
    app = app or create_app(os.getenv('FLASK_CONFIG') or 'default')
    celery = Celery(__name__, broker=app.config['CELERY_BROKER_URL'], include=['app.query'])
    celery.conf.update(app.config)
    TaskBase = celery.Task

    class ContextTask(TaskBase):
        abstract = True
        def __call__(self, *args, **kwargs):
            with app.app_context():
                return TaskBase.__call__(self, *args, **kwargs)

    celery.Task = ContextTask
    return celery

当我经营芹菜工人时,我得到错误:

File "/home/yx/Documents/flasky/venv/bin/celery", line 11, in <module>
    sys.exit(main())
  File "/home/yx/Documents/flasky/venv/local/lib/python2.7/site-packages/celery/__main__.py", line 30, in main
    main()
  File "/home/yx/Documents/flasky/venv/local/lib/python2.7/site-packages/celery/bin/celery.py", line 81, in main
    cmd.execute_from_commandline(argv)
  File "/home/yx/Documents/flasky/venv/local/lib/python2.7/site-packages/celery/bin/celery.py", line 770, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "/home/yx/Documents/flasky/venv/local/lib/python2.7/site-packages/celery/bin/base.py", line 309, in execute_from_commandline
    argv = self.setup_app_from_commandline(argv)
  File "/home/yx/Documents/flasky/venv/local/lib/python2.7/site-packages/celery/bin/base.py", line 469, in setup_app_from_commandline
    self.app = self.find_app(app)
  File "/home/yx/Documents/flasky/venv/local/lib/python2.7/site-packages/celery/bin/base.py", line 489, in find_app
    return find_app(app, symbol_by_name=self.symbol_by_name)
  File "/home/yx/Documents/flasky/venv/local/lib/python2.7/site-packages/celery/app/utils.py", line 229, in find_app
    sym = symbol_by_name(app, imp=imp)
  File "/home/yx/Documents/flasky/venv/local/lib/python2.7/site-packages/celery/bin/base.py", line 492, in symbol_by_name
    return symbol_by_name(name, imp=imp)
  File "/home/yx/Documents/flasky/venv/local/lib/python2.7/site-packages/kombu/utils/__init__.py", line 96, in symbol_by_name
    module = imp(module_name, package=package, **kwargs)
  File "/home/yx/Documents/flasky/venv/local/lib/python2.7/site-packages/celery/utils/imports.py", line 101, in import_from_cwd
    return imp(module, package=package)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/yx/Documents/flasky/app/query.py", line 6, in <module>
    celery = create_celery_app()
  File "/home/yx/Documents/flasky/app/__init__.py", line 29, in create_celery_app
    app = app or create_app(os.getenv('FLASK_CONFIG') or 'default')
  File "/home/yx/Documents/flasky/app/__init__.py", line 24, in create_app
    from main import main as main_blueprint
  File "/home/yx/Documents/flasky/app/main/__init__.py", line 5, in <module>
    from . import views, errors
  File "/home/yx/Documents/flasky/app/main/views.py", line 7, in <module>
    from ..query import user_query
ImportError: cannot import name user_query

但是user_query来自query.py,所以有人知道我是否错过了什么? 谢谢。

0 个答案:

没有答案