gpt4 book ai didi

Python + Flask + Mysql 错误 2006(服务器已经消失)

转载 作者:行者123 更新时间:2023-11-29 15:20:23 26 4
gpt4 key购买 nike

我有一个带有 python、flask 和flask_mysqldb 的应用程序。当我执行第一个查询时,一切正常,但第二个查询总是抛出错误(2006,服务器已经消失)。我在网上找到的所有内容都表明此错误是超时问题,但这似乎不是我的情况,因为:

1 - 我在运行第一个查询后几秒钟运行第二个查询

2 - 我的超时配置设置为 8 小时

我不知道这可能是什么,这是我正在运行的代码:

import os
from flask import Flask
from flask import render_template
from flaskext.mysql import MySQL
import endpoints.usuario as usuario

app = Flask(__name__, static_folder='/root/sftp/atom-projects/flask-example/public/')

app.config['MYSQL_HOST'] = '123'
app.config['MYSQL_USER'] = '123'
app.config['MYSQL_PASSWORD'] = '123'
app.config['MYSQL_DB'] = '123'

app.add_url_rule('/usuarios', 'usuarios', usuario.list_all, methods=['GET'])

@app.errorhandler(404)
def not_found(e):
return app.send_static_file('index.html')

这是 usuarios 文件的代码:

from flask_mysqldb import MySQL
from flask import Flask, make_response
from flask import current_app
from flask import request
import bcrypt

def list_all():

mysql = MySQL(current_app)
cursor = mysql.connection.cursor()

cursor.execute("select * from usuario")
records = cursor.fetchall()
usuarios = []
for row in records:
usuarios.append({"id":row[0], "nome":row[1], "email":row[2], "senha":row[3], "tipo":row[4]})

for usuario in usuarios:
tipo = None
cursor.execute("select * from tipo_usuario where id = %s", [usuario['tipo']])
records = cursor.fetchall()
for row in records:
usuario['tipo'] = {"id":row[0], "permissao":row[1]}

return make_response({"msg":'', "error":False, "data":usuarios})

我在 nginx + Gunicorn 上运行这个,这是日志:

gunicorn -w 1 --reload main:app
[2019-12-19 12:53:21 +0000] [5356] [INFO] Starting gunicorn 20.0.4
[2019-12-19 12:53:21 +0000] [5356] [INFO] Listening at: http://127.0.0.1:8000 (5356)
[2019-12-19 12:53:21 +0000] [5356] [INFO] Using worker: sync
[2019-12-19 12:53:21 +0000] [5359] [INFO] Booting worker with pid: 5359
[2019-12-19 12:53:28 +0000] [5359] [ERROR] Error handling request /usuarios
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/gunicorn/workers/sync.py", line 134, in handle
self.handle_request(listener, req, client, addr)
File "/usr/local/lib/python3.5/dist-packages/gunicorn/workers/sync.py", line 175, in handle_request
respiter = self.wsgi(environ, resp.start_response)
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 2457, in wsgi_app
ctx.auto_pop(error)
File "/usr/local/lib/python3.5/dist-packages/flask/ctx.py", line 452, in auto_pop
self.pop(exc)
File "/usr/local/lib/python3.5/dist-packages/flask/ctx.py", line 438, in pop
app_ctx.pop(exc)
File "/usr/local/lib/python3.5/dist-packages/flask/ctx.py", line 238, in pop
self.app.do_teardown_appcontext(exc)
File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 2320, in do_teardown_appcontext
func(exc)
File "/usr/local/lib/python3.5/dist-packages/flask_mysqldb/__init__.py", line 100, in teardown
ctx.mysql_db.close()
MySQLdb._exceptions.OperationalError: (2006, '')

如果我用更多的工作人员运行它,我可以运行更多(取决于有多少工作人员)查询,这可能是什么原因造成的?

最佳答案

我的 python Flask 应用程序上也有类似的错误。当我尝试使用 fromflask import current_app as app 时,我仍然收到错误。最终,我设法通过为 Flask 应用程序对象 app = Flask(__name__) 创建一个单独的文件,然后将其导入到需要该对象的所有文件中来消除错误。

例如。

应用程序对象模块:main.py

from flask import Flask, request

app = Flask(__name__)

其他文件,例如usuarios.pyconfig.py

from main import app
from flask_mysqldb import MySQL

app.config['MYSQL_HOST'] = '123'
app.config['MYSQL_USER'] = '123'
app.config['MYSQL_PASSWORD'] = '123'
app.config['MYSQL_DB'] = '123'

mysql = MySQL(app)

注意:我使用了 flask_mysqldb 而不是 flaskext.mysql 但我认为只要您使用两者的参数都正确。

关于Python + Flask + Mysql 错误 2006(服务器已经消失),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59410455/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com