【发布时间】:2020-04-12 02:15:28
【问题描述】:
我有一个包含 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, '')
如果我使用更多工作人员运行它,我可以运行更多(取决于有多少工作人员)查询,这可能是什么原因造成的?
【问题讨论】: