Flask Debug抛出多线程sqlite3.ProgrammingError

时间:2016-12-22 14:26:54

标签: python multithreading debugging sqlite sqlalchemy

一个简单的Python Flask SQLAchemy应用程序可以与debug=False一起使用,但在debug=True访问数据库时会抛出多线程错误。似乎调试重新启动应用程序创建两个线程。解决方法似乎是在每次查询之前连接到数据库。我相信有一种更优雅的方式可以通过'来解决问题。但是想在这个阶段保持这个简单。

这是Flask错误还是实施问题?我怀疑后者所以任何指导都赞赏。

这是一个简单版本的程序,它将db查询输出到控制台:

from sqlalchemy import create_engine, MetaData, select, Table
from flask import Flask


# create application
app = Flask(__name__)

sqlite_file = 'bankapi_db.sqlite'
sqlalchemy_dbpath = "sqlite:///" + sqlite_file


# Connecting to the database file
engine = create_engine(sqlalchemy_dbpath)
meta = MetaData()
conn = engine.connect()
accountz = Table('accounts', meta, autoload=True, autoload_with=engine)


print('+++++++++++++++++++++++++++++++++++++++++++++++++')


##########################################################################
# api_info
##########################################################################


@app.route('/')
def api_info():
    '''Display information'''

    # conn = engine.connect()

    s = select([accountz])
    rv = conn.execute(s)
    for row in rv:
        print(row)

    # conn.close()
    return "back at you"


if __name__ == '__main__':
    app.run(debug=False)

使用debug=True,您可以在控制台中看到重启(++++++++):

$ python3 thread_test.py
+++++++++++++++++++++++++++++++++++++++++++++++++
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
+++++++++++++++++++++++++++++++++++++++++++++++++
 * Debugger is active!
 * Debugger pin code: 738-768-392

Versus Debug=False

 $ python3 thread_test.py
+++++++++++++++++++++++++++++++++++++++++++++++++
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

0 个答案:

没有答案