Flask、Gunicorn、Mysqlconnctor /GET 每次刷新时出错
Flask, Gunicorn, Mysqlconnctor /GET error on every refresh
我正在制作一个非常基本的 Flask 应用程序以在 vps.
上显示 mysql 数据库的表
我通过 gunicorn 运行 应用程序,它工作正常并显示表格,但每次我在应用程序中刷新浏览器时,它都会中断并 returns 下面的错误。
如何解决此问题并使浏览器刷新在我的应用程序中起作用?
我的所有文件都在下面
GUNICORN 错误
:8880 wsgi:app
[2020-04-16 13:23:34 +0000] [3196] [INFO] Starting gunicorn 20.0.4
[2020-04-16 13:23:34 +0000] [3196] [INFO] Listening at: http://0.0.0.0:8880 (319
6)
[2020-04-16 13:23:34 +0000] [3196] [INFO] Using worker: sync
[2020-04-16 13:23:34 +0000] [3199] [INFO] Booting worker with pid: 3199
[2020-04-16 13:23:37 +0000] [3196] [INFO] Handling signal: winch
[2020-04-16 13:23:43,615] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2447, in wsgi
_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1952, in full
_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1821, in hand
le_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 39, in re
raise
raise value
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1950, in full
_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1936, in disp
atch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/var/www/pi.sminfo.me/app.py", line 7, in display_tables
cursor.execute("SELECT * FROM pitemp;")
File "/home/ubuntu/.local/lib/python3.6/site-packages/mysql/connector/cursor_c
ext.py", line 233, in execute
raise errors.ProgrammingError("Cursor is not connected")
mysql.connector.errors.ProgrammingError: Cursor is not connected
[2020-04-16 13:23:47 +0000] [3196] [INFO] Handling signal: winch
[2020-04-16 13:24:46 +0000] [3196] [INFO] Handling signal: winch
^C[2020-04-16 13:28:30 +0000] [3196] [INFO] Handling signal: int
[2020-04-16 13:28:30 +0000] [3199] [INFO] Worker exiting (pid: 3199)
[2020-04-16 13:28:30 +0000] [3196] [INFO] Shutting down: Master
APP.PY
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def display_tables():
cursor.execute("SELECT * FROM pitemp;")
data = cursor.fetchall()
db.close()
cursor.close()
return render_template("index.html", data=data)
db2.py
db = mysql.connector.connect (
host="localhost",
user="********",
passwd="*******",
database="*******"
)
cursor = db.cursor()
wgsi.py
if __name__ == "__main__":
app.run()
/templates/index.html
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rpi Temp Log</title>
<style>
h1 {
color: #C93F3F;
font-family: Verdana,Geneva,sans-serif;
font-size: 30px;
text-align: left;
}
table {
border-collapse: collapse;
width: 50%;
color: #C93F3F;
font-family: monospace;
font-size: 25px;
text-align: left;
}
th {
background-color: #3A3AEA;
color: white;
}
tr:nth-child(even) {background-color: #f2f2f2}
</style>
</head>
<body>
<div><img src="https://www.raspberrypi.org/app/uploads/2011/10/Raspi-PGB001.png" width="200" height="200" align="left"/><h1>Raspberry Pi Temperature Logger </h1></div>
<table border="1" cellpadding="5" cellspacing="5">
{% for row in data %}
<tr>
{% for d in row %}
<td>{{ d }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</html>
第一个问题是您直接关闭视图中的连接:
@app.route('/')
def display_tables():
cursor.execute("SELECT * FROM pitemp;")
data = cursor.fetchall()
db.close() # CLOSED HERE
cursor.close()
return render_template("index.html", data=data)
这就是为什么多次访问该路由会抱怨您正在尝试使用已关闭的连接。
另外,您不能以这种方式维护数据库连接。您还没有展示连接是如何实际建立的——您是否将此设置为您的应用程序的 global
? Gunicorn 将产生多个进程,每个进程可能都有自己的全局连接,或者连接可能是预先分叉的。无论哪种方式,这都会使调试或处理变得混乱。
您应该查看 flask-sqlalchemy
if you are just starting out, as that will ensure that your connection and sessions are managed each time a route is visited. Alternatively, you might want to take the approach of Flask and SQLAlchemy without the Flask-SQLAlchemy Extension 或类似内容。如果您不想为此使用 ORM,则需要考虑如何在没有全局连接的情况下建立和断开连接。
我正在制作一个非常基本的 Flask 应用程序以在 vps.
上显示 mysql 数据库的表我通过 gunicorn 运行 应用程序,它工作正常并显示表格,但每次我在应用程序中刷新浏览器时,它都会中断并 returns 下面的错误。
如何解决此问题并使浏览器刷新在我的应用程序中起作用? 我的所有文件都在下面
GUNICORN 错误
:8880 wsgi:app
[2020-04-16 13:23:34 +0000] [3196] [INFO] Starting gunicorn 20.0.4
[2020-04-16 13:23:34 +0000] [3196] [INFO] Listening at: http://0.0.0.0:8880 (319
6)
[2020-04-16 13:23:34 +0000] [3196] [INFO] Using worker: sync
[2020-04-16 13:23:34 +0000] [3199] [INFO] Booting worker with pid: 3199
[2020-04-16 13:23:37 +0000] [3196] [INFO] Handling signal: winch
[2020-04-16 13:23:43,615] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2447, in wsgi
_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1952, in full
_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1821, in hand
le_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 39, in re
raise
raise value
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1950, in full
_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1936, in disp
atch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/var/www/pi.sminfo.me/app.py", line 7, in display_tables
cursor.execute("SELECT * FROM pitemp;")
File "/home/ubuntu/.local/lib/python3.6/site-packages/mysql/connector/cursor_c
ext.py", line 233, in execute
raise errors.ProgrammingError("Cursor is not connected")
mysql.connector.errors.ProgrammingError: Cursor is not connected
[2020-04-16 13:23:47 +0000] [3196] [INFO] Handling signal: winch
[2020-04-16 13:24:46 +0000] [3196] [INFO] Handling signal: winch
^C[2020-04-16 13:28:30 +0000] [3196] [INFO] Handling signal: int
[2020-04-16 13:28:30 +0000] [3199] [INFO] Worker exiting (pid: 3199)
[2020-04-16 13:28:30 +0000] [3196] [INFO] Shutting down: Master
APP.PY
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def display_tables():
cursor.execute("SELECT * FROM pitemp;")
data = cursor.fetchall()
db.close()
cursor.close()
return render_template("index.html", data=data)
db2.py
db = mysql.connector.connect (
host="localhost",
user="********",
passwd="*******",
database="*******"
)
cursor = db.cursor()
wgsi.py
if __name__ == "__main__":
app.run()
/templates/index.html
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rpi Temp Log</title>
<style>
h1 {
color: #C93F3F;
font-family: Verdana,Geneva,sans-serif;
font-size: 30px;
text-align: left;
}
table {
border-collapse: collapse;
width: 50%;
color: #C93F3F;
font-family: monospace;
font-size: 25px;
text-align: left;
}
th {
background-color: #3A3AEA;
color: white;
}
tr:nth-child(even) {background-color: #f2f2f2}
</style>
</head>
<body>
<div><img src="https://www.raspberrypi.org/app/uploads/2011/10/Raspi-PGB001.png" width="200" height="200" align="left"/><h1>Raspberry Pi Temperature Logger </h1></div>
<table border="1" cellpadding="5" cellspacing="5">
{% for row in data %}
<tr>
{% for d in row %}
<td>{{ d }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</html>
第一个问题是您直接关闭视图中的连接:
@app.route('/')
def display_tables():
cursor.execute("SELECT * FROM pitemp;")
data = cursor.fetchall()
db.close() # CLOSED HERE
cursor.close()
return render_template("index.html", data=data)
这就是为什么多次访问该路由会抱怨您正在尝试使用已关闭的连接。
另外,您不能以这种方式维护数据库连接。您还没有展示连接是如何实际建立的——您是否将此设置为您的应用程序的 global
? Gunicorn 将产生多个进程,每个进程可能都有自己的全局连接,或者连接可能是预先分叉的。无论哪种方式,这都会使调试或处理变得混乱。
您应该查看 flask-sqlalchemy
if you are just starting out, as that will ensure that your connection and sessions are managed each time a route is visited. Alternatively, you might want to take the approach of Flask and SQLAlchemy without the Flask-SQLAlchemy Extension 或类似内容。如果您不想为此使用 ORM,则需要考虑如何在没有全局连接的情况下建立和断开连接。