Flask-SQLAlchemy 的 Flask-socketio 上下文

Flask-socketio context for Flask-SQLAlchemy

我正在使用 Flask、Flask-Socketio 和 Flask-SQLAlchemy 创建实时报告应用程序。我当前的设计在连接上创建了一个后台线程,它查询 API 并插入到应用程序数据中。但是,当 运行 这个时,我得到错误

RuntimeError: No application found. Either work inside a view function or push an application context.

flask_react_app.py:

from threading import Lock
from flask import Blueprint, render_template
from .model import Stock
from . import socketio

main = Blueprint('main', __name__)
thread_lock = Lock()
thread = None


@main.route('/')
def index():
    """Serve client-side application."""
    return render_template('index.html', async_mode=socketio.async_mode)


def generate_data():
    """
    returns the Stock object query set
    """
    return [i.serialize for i in Stock.query.all()]


def background_thread():
    """Example of how to send server generated events to clients."""
    while True:
        socketio.sleep(10)
        socketio.emit("my_response", generate_data())


@socketio.on("connect")
def test_connect():
    """
    Connect method which fires off thread to notify worker to get data.
    :return: emits initial data.
    """

    global thread
    with thread_lock:
        if thread is None:
            thread = socketio.start_background_task(target=background_thread)
    socketio.emit("my_response", generate_data())

我有两个问题。首先提到的错误,其次,必须有更好的方法!

您的问题与 Flask-SocketIO 无关,但要在后台线程中使用 Flask-SQLAlchemy 您需要一个应用程序上下文。

尝试以下操作:

def background_thread(app):
    """Example of how to send server generated events to clients."""
    with app.app_context():
        while True:
            socketio.sleep(10)
            socketio.emit("my_response", generate_data())

然后在启动后台线程的地方将应用程序实例作为参数传递:

thread = socketio.start_background_task(target=background_thread, args=(current_app._get_current_object(),))

Miguel 的回答是正确的,但如果两个关键字参数都传递给函数,则会引发下一个异常:

TypeError: background_thread() got an unexpected keyword argument 'args'

所以你只需要通过

thread = socketio.start_background_task(background_thread, (current_app._get_current_object()))