- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Flask Rest api 应用程序,具有以下设置。
已安装的软件包
alembic==1.3.0
aniso8601==8.0.0
astroid==2.3.3
attrs==19.3.0
bcrypt==3.1.7
cffi==1.13.2
Click==7.0
colorama==0.4.1
Flask==1.1.1
Flask-Bcrypt==0.7.1
Flask-Migrate==2.5.2
flask-restplus==0.13.0
Flask-Script==2.0.6
Flask-SQLAlchemy==2.4.1
Flask-Testing==0.7.1
importlib-metadata==0.23
isort==4.3.21
itsdangerous==1.1.0
Jinja2==2.10.3
jsonschema==3.1.1
lazy-object-proxy==1.4.3
Mako==1.1.0
MarkupSafe==1.1.1
mccabe==0.6.1
more-itertools==7.2.0
mysqlclient==1.4.5
pycparser==2.19
PyJWT==1.7.1
pylint==2.4.3
PyMySQL==0.9.3
pyrsistent==0.15.5
python-dateutil==2.8.1
python-editor==1.0.4
pytz==2019.3
six==1.13.0
SQLAlchemy==1.3.11
Werkzeug==0.16.0
wrapt==1.11.2
zipp==0.6.0
我有以下生产配置,并将调试启用为 true 以了解堆栈跟踪。
class ProductionConfig(Config):
DEBUG = True
user = 'xxxxxx@xxxxxx-mysqldbserver'
passs = 'xxxxxx'
host = 'xxxxxx-mysqldbserver.mysql.database.azure.com'
db = 'ifsc'
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://{0}:{1}@{2}/{3}'.format(user, passs, host, db)
我有 __init__.py
初始化 SQLAlchemy 如下
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from .config import config_by_name
db = SQLAlchemy()
flask_bcrypt = Bcrypt()
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config_by_name[config_name])
db.init_app(app)
flask_bcrypt.init_app(app)
return app
我有一个 app.py,其中包含实际运行 Flask 应用程序的以下代码。应用程序.py
from flask_cors import CORS, cross_origin
from api import blueprint
from api.main import create_app, db
from api.main.seed import seed_db
app = create_app(os.getenv('BOILERPLATE_ENV') or 'prod') # dev|prod
CORS(app)
app.register_blueprint(blueprint)
app.app_context().push()
if __name__ == '__main__':
app.run()
问题:我遇到了 mysql 连接问题,在 azure 的数据库上出现以下错误。我尝试将 max_allowed_packet
服务器参数设置为 1073741824
。但仍然看到这个错误。
症状:对于全新部署,它会做出响应,如果 API 空闲 5 到 10 分钟并发出相同的请求,则会出现以下错误。 60 分钟后它再次开始工作,并且该行为仍在继续。
堆栈跟踪:
sqlalchemy.exc.OperationalError sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2006, "MySQL server has gone away (ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))") [SQL: SELECT bank_details.id AS bank_details_id, bank_details.bank AS bank_details_bank, bank_details.ifsc AS bank_details_ifsc, bank_details.branch AS bank_details_branch, bank_details.address AS bank_details_address, bank_details.district AS bank_details_district, bank_details.city AS bank_details_city, bank_details.state AS bank_details_state, bank_details.phone AS bank_details_phone, bank_details.micr AS bank_details_micr, bank_details.pin AS bank_details_pin FROM bank_details WHERE bank_details.bank = %(bank_1)s AND bank_details.city = %(city_1)s] [parameters: {'bank_1': 'axis bank', 'city_1': 'goa'}] (Background on this error at: http://sqlalche.me/e/e3q8)
Traceback (most recent call last) File "E:\sampled\sample_api\env\Lib\site-packages\pymysql\connections.py", line 713, in _write_bytes self._sock.sendall(data) During handling of the above exception, another exception occurred: File "E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\engine\base.py", line 1245, in _execute_context self.dialect.do_execute( File "E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\engine\default.py", line 581, in do_execute cursor.execute(statement, parameters) File "E:\sampled\sample_api\env\Lib\site-packages\pymysql\cursors.py", line 170, in execute result = self._query(query) File "E:\sampled\sample_api\env\Lib\site-packages\pymysql\cursors.py", line 328, in _query conn.query(q) File "E:\sampled\sample_api\env\Lib\site-packages\pymysql\connections.py", line 516, in query self._execute_command(COMMAND.COM_QUERY, sql) File "E:\sampled\sample_api\env\Lib\site-packages\pymysql\connections.py", line 771, in _execute_command self._write_bytes(packet) File "E:\sampled\sample_api\env\Lib\site-packages\pymysql\connections.py", line 716, in _write_bytes raise err.OperationalError( The above exception was the direct cause of the following exception: File "E:\sampled\sample_api\env\Lib\site-packages\flask\app.py", line 2463, in call return self.wsgi_app(environ, start_response) File "E:\sampled\sample_api\env\Lib\site-packages\flask\app.py", line 2449, in wsgi_app response = self.handle_exception(e) File "E:\sampled\sample_api\env\Lib\site-packages\flask_restplus\api.py", line 584, in error_router return original_handler(e) File "E:\sampled\sample_api\env\Lib\site-packages\flask\app.py", line 1866, in handle_exception reraise(exc_type, exc_value, tb) File "E:\sampled\sample_api\env\Lib\site-packages\flask_compat.py", line 38, in reraise raise value.with_traceback(tb) File "E:\sampled\sample_api\env\Lib\site-packages\flask\app.py", line 2446, in wsgi_app response = self.full_dispatch_request() File "E:\sampled\sample_api\env\Lib\site-packages\flask\app.py", line 1951, in full_dispatch_request rv = self.handle_user_exception(e) File "E:\sampled\sample_api\env\Lib\site-packages\flask_restplus\api.py", line 584, in error_router return original_handler(e) File "E:\sampled\sample_api\env\Lib\site-packages\flask\app.py", line 1820, in handle_user_exception reraise(exc_type, exc_value, tb) File "E:\sampled\sample_api\env\Lib\site-packages\flask_compat.py", line 38, in reraise raise value.with_traceback(tb) File "E:\sampled\sample_api\env\Lib\site-packages\flask\app.py", line 1949, in full_dispatch_request rv = self.dispatch_request() File "E:\sampled\sample_api\env\Lib\site-packages\flask\app.py", line 1935, in dispatch_request return self.view_functionsrule.endpoint File "E:\sampled\sample_api\env\Lib\site-packages\flask_restplus\api.py", line 325, in wrapper resp = resource(*args, **kwargs) File "E:\sampled\sample_api\env\Lib\site-packages\flask\views.py", line 89, in view return self.dispatch_request(*args, **kwargs) File "E:\sampled\sample_api\env\Lib\site-packages\flask_restplus\resource.py", line 44, in dispatch_request resp = meth(*args, **kwargs) File "E:\sampled\sample_api\env\Lib\site-packages\flask_restplus\marshalling.py", line 243, in wrapper resp = f(*args, **kwargs) File "E:\sampled\sample_api\api\main\controller\ifsc_controller.py", line 37, in get banks = get_banks_by_name_city(bank, city) File "E:\sampled\sample_api\api\main\service\ifsc_service.py", line 28, in get_banks_by_name_city return Ifsc.query.filter_by(bank = name, city = city).all() File "E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\orm\query.py", line 3211, in all return list(self) File "E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\orm\query.py", line 3367, in iter return self._execute_and_instances(context) File "E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\orm\query.py", line 3392, in _execute_and_instances result = conn.execute(querycontext.statement, self._params) File "E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\engine\base.py", line 982, in execute return meth(self, multiparams, params) File "E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\sql\elements.py", line 287, in _execute_on_connection return connection._execute_clauseelement(self, multiparams, params) File "E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\engine\base.py", line 1095, in _execute_clauseelement ret = self._execute_context( File "E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\engine\base.py", line 1249, in _execute_context self._handle_dbapi_exception( File "E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\engine\base.py", line 1476, in _handle_dbapi_exception util.raise_from_cause(sqlalchemy_exception, exc_info) File "E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\util\compat.py", line 398, in raise_from_cause reraise(type(exception), exception, tb=exc_tb, cause=cause) File "E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\util\compat.py", line 152, in reraise raise value.with_traceback(tb) File "E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\engine\base.py", line 1245, in _execute_context self.dialect.do_execute( File "E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\engine\default.py", line 581, in do_execute cursor.execute(statement, parameters) File "E:\sampled\sample_api\env\Lib\site-packages\pymysql\cursors.py", line 170, in execute result = self._query(query) File "E:\sampled\sample_api\env\Lib\site-packages\pymysql\cursors.py", line 328, in _query conn.query(q) File "E:\sampled\sample_api\env\Lib\site-packages\pymysql\connections.py", line 516, in query self._execute_command(COMMAND.COM_QUERY, sql) File "E:\sampled\sample_api\env\Lib\site-packages\pymysql\connections.py", line 771, in _execute_command self._write_bytes(packet) File "E:\sampled\sample_api\env\Lib\site-packages\pymysql\connections.py", line 716, in _write_bytes raise err.OperationalError( sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2006, "MySQL server has gone away (ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))") [SQL: SELECT bank_details.id AS bank_details_id, bank_details.bank AS bank_details_bank, bank_details.ifsc AS bank_details_ifsc, bank_details.branch AS bank_details_branch, bank_details.address AS bank_details_address, bank_details.district AS bank_details_district, bank_details.city AS bank_details_city, bank_details.state AS bank_details_state, bank_details.phone AS bank_details_phone, bank_details.micr AS bank_details_micr, bank_details.pin AS bank_details_pin FROM bank_details WHERE bank_details.bank = %(bank_1)s AND bank_details.city = %(city_1)s] [parameters: {'bank_1': 'axis bank', 'city_1': 'goa'}] (Background on this error at: http://sqlalche.me/e/e3q8) The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error. To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.
You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:
dump() shows all variables in the frame dump(obj) dumps all that's known about the object
最佳答案
经过 4 个月的研究和了解 Flask 的工作原理后,我通过推送 app_context 来初始化数据库进行了以下更改,解决了问题。
with app.app_context():
db.init_app(app)
注意事项:我们必须确保应用程序上下文与 sqlalchemy 数据库初始化一起推送。在发布的问题中,我一直在初始化数据库,然后稍后推送应用程序上下文app.app_context().push()
。
关于python - MySQL 服务器已消失 ConnectionResetError 10054 Flask SqlAlchemy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59018013/
我正在遵循Flask-potion教程并尝试保护我的api。我一直试图找出如何在User端点上设置权限,以使一个User无法查看另一个User的记录。这是我的代码: class UserResourc
项目设置 Python 3.5.3 flask 0.12.2 目录 . ├── Core │ ├── BackgroundProcessManager.py │ ├── FirebaseDat
我正在构建 flask 应用程序并决定这次尝试应用程序工厂方法,但是遇到了 flask 迁移的麻烦并且无法找出简单的解决方案。 请注意,我想将配置位置作为选项传递给脚本 管理.py: manager
我尝试通过以下示例代码来熟悉Flask-APScheduler插件:https://github.com/viniciuschiele/flask-apscheduler/blob/master/ex
我一周前刚刚了解了 Flask 和 JWT,所以我真的需要一些帮助。有人可以解释一下flask-jwt和flask-jwt-extended之间的区别吗? 最佳答案 Flask-JWT 没有那么多功能
我是 Flask 的新手,我只是想用它创建一个简单的市场。我的网站上有三种类型的用户 [卖家、买家、管理员]。我认为每种类型的用户都应该有自己的注册和登录,因为他们的角色应该提供不同类型的信息。另一方
基本上,我想做一些非常简单的事情:我想创建一个用于删除数据库中条目的表单。 模板正在创建一个包含所有条目的html表,没有任何麻烦。我现在的问题是:如何将其转换为每行都有链接的表单。 当然,我可以通过
我对Flask来说非常陌生,其中一个基本要求是我需要SEO友好的URL。 我说一条路线 @app.route('/sales/') @app.route(/sales/) def get_sales(
一直在为用户管理系统开发Flask-user,但是我看不到自定义登录页面和注册页面的方法。我使用pip安装了flask-user pip install flask-user 任何帮助,将不胜感激 最
我有一个奇怪的情况,不知道如何找到问题。 我已经创建了一个这样的蓝图 reportjobsmod = Blueprint('jobreports', __name__, url_prefix='/jo
我认为在 Flask 中拥有实例变量的正确方法是添加用户和 session ,但我正在尝试测试一个概念,我还不想经历所有这些。我试图让一个网络应用程序将图像加载到一个变量中,然后可以对其执行不同的图像
select = SelectField("City") form.select.choices = [(places['name'], places['name']) for places in p
这个问题在这里已经有了答案: Provide extra information to Flask's app.logger (2 个回答) 3年前关闭。 我正在运行一个 flask 应用程序。我想写
我当时正在编程一个非常不错的api,使用json从游戏服务器到我的网站空间获取一些json数据, 但是每次我使用angular发送请求时,我都会得到以下信息:127.0.0.1--[20 / Mar
我已经开发了一个上传表单,以将特定的.xlsx文件作为上传。要求是要处理非xlsx上载的任何异常(例如zip,exe文件)。我正在使用pyexcel库读取上传内容。我尝试创建以下代码来处理此异常: e
我使用此功能在网站上捕获404错误: @app.errorhandler(404) def page_not_found(e): logger.warning('User raised an
我正在尝试同时使用 Flask Restful 和 Flask SocketIO。我已经制作了一个 Flask Restful 应用程序,但现在我想使用 Flask SocketIO 在客户端和我的服
我有一些 Flask 应用程序,我通过设置 FLASK_APP 来运行它们。环境变量和调用 flask run .自 FLASK_DEBUG也设置了,每当我更改一些代码时,应用程序都可以方便地重新加载
我目前正在使用 Flask 构建一个应用程序。我正在努力访问 Flask 实例“app”以及 Flask-mail 实例“mail”。 下面是我的项目的样子: └── my-project ├
Remember-me是 parameter flask_login.login_user(user, remember=False, duration=None, force=False, fres
我是一名优秀的程序员,十分优秀!