尝试使用SQLAlchemy和SQLITE3以“登录”形式部署Flask Web应用程序。该应用程序在本地运行时的行为符合预期。但是,当部署为Azure Web Service时,将无法访问或写入数据库。这样会导致以下错误输出:
500 Internal Server Error
2019-01-05T19:13:30.235301217Z [2019-01-05 19:13:30,198] ERROR in app: Exception on /register [POST]
2019-01-05T19:13:30.235339017Z Traceback (most recent call last):
2019-01-05T19:13:30.235343817Z File
...
2019-01-05T19:13:30.235502017Z File "/home/site/wwwroot/antenv/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 724, in _commit_impl
2019-01-05T19:13:30.235505417Z self.engine.dialect.do_commit(self.connection)
2019-01-05T19:13:30.235508817Z File "/home/site/wwwroot/antenv/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 462, in do_commit
2019-01-05T19:13:30.235512317Z dbapi_connection.commit()
2019-01-05T19:13:30.235624616Z sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) database is locked (Background on this error at: http://sqlalche.me/e/e3q8)
我在调查此错误时遇到了麻烦,因为它在本地的行为符合预期。 Azure Web App位于其他人不可见的资源组中,因此它不能成为并发冲突的根源。
models.py
from flask_login import UserMixin
from werkzeug.security import check_password_hash
from werkzeug.security import generate_password_hash
from app.extensions import db
from app.extensions import login
@login.user_loader
def load_user(id):
return User.query.get(int(id))
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True, unique=True)
password_hash = db.Column(db.String(128))
def set_password(self, password):
self.password_hash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password_hash, password)
def __repr__(self):
return '<User {}>'.format(self.username)
webapp.py
from flask import Blueprint
from flask import redirect
from flask import render_template
from flask import request
from flask import url_for
from flask_login import current_user
from flask_login import login_required
from flask_login import login_user
from flask_login import logout_user
from werkzeug.urls import url_parse
from app.extensions import db
from app.forms import LoginForm
from app.forms import RegistrationForm
from app.models import User
server_bp = Blueprint('main', __name__)
@server_bp.route('/')
def index():
return render_template("index.html", title='Home Page')
@server_bp.route('/login', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
return redirect(url_for('main.index'))
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user is None or not user.check_password(form.password.data):
error = 'Invalid username or password'
return render_template('login.html', form=form, error=error)
login_user(user, remember=form.remember_me.data)
next_page = request.args.get('next')
if not next_page or url_parse(next_page).netloc != '':
next_page = url_for('main.index')
return redirect(next_page)
return render_template('login.html', title='Sign In', form=form)
@server_bp.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('main.index'))
@server_bp.route('/register', methods=['GET', 'POST'])
def register():
if current_user.is_authenticated:
return redirect(url_for('main.index'))
form = RegistrationForm()
if form.validate_on_submit():
user = User(username=form.username.data)
user.set_password(form.password.data)
db.session.add(user)
db.session.commit()
return redirect(url_for('main.login'))
return render_template('register.html', title='Register', form=form)
@server_bp.route('/dynamic_chart', methods=['POST', "GET"])
def dynamicchart():
req_data = request.get_json()
hour1 = req_data["hour1"]
return hour1
任何想法,我们都会给予帮助。谢谢您的宝贵时间。
答案 0 :(得分:0)
我不会将SQLite用于生产。我也遇到了这个错误,并更改为PostgreSQL。此后,此错误不再出现。