0

在此处输入图像描述

我正在尝试使用 python huey ( https://github.com/coleifer/huey/blob/master/huey/api.py ) 来允许使用带有烧瓶的任务队列。

我的应用程序/初始化包含:

from flask_sqlalchemy import SQLAlchemy

mail = Mail()
db = SQLAlchemy()



def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    # not using sqlalchemy event system, hence disabling it

    config[config_name].init_app(app)

    # Set up tasking
    mail.init_app(app)
    db.init_app(app)
    ......

    return app


def create_huey_app():
    _app = Flask('huey-app')
    db.init_app(_app)

    from app.models.other_models import Postings, Accounts

    return _app

我的 Huey 任务文件夹包含:

@my_huey.task()
def create_listing_task(messages):
    """Background task to send an email with Flask-Mail."""
    h_app = create_huey_app()
    with h_app.app_context():
        create_listing(messages)

在我的烧瓶视图中,我有:

@main.route('/selected')
def selected():

....

    res  = create_listing_task(messages)
    res(blocking=True, timeout=5)  # Block for up to 5 seconds

    return 'fin'

我得到的错误是

Traceback (most recent call last):
  File "...\lib\site-packages\flask\app.py", line 1994, in __call__
    return self.wsgi_app(environ, start_response)
  File "...\lib\site-packages\flask\app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "...\lib\site-packages\flask\app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "...\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "...\lib\site-packages\flask\app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "...\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "...\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "...\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "...\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "...\lib\site-packages\flask\app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "...\app\main\views.py", line 99, in selected
    res(blocking=True, timeout=100)  # Block for up to 5 seconds
  File "...\lib\site-packages\huey\api.py", line 701, in __call__
    return self.get(*args, **kwargs)
  File "...\lib\site-packages\huey\api.py", line 743, in get
    raise TaskException(result.metadata)
huey.exceptions.TaskException: OperationalError('(sqlite3.OperationalError) no such table: postings',)

尽管我正在初始化 flask-sqlalchemy 扩展,但应用程序似乎找不到表。我怎样才能得到这个工作?

4

1 回答 1

2

表不会自动创建。你需要执行db.create_all(). 请参阅 Flask-SQLAlchemy快速入门

于 2018-08-26T20:30:18.783 回答