【问题标题】:Flask, Gunicorn, Mysqlconnctor /GET error on every refreshFlask、Gunicorn、Mysqlconnctor /GET 每次刷新都会出错
【发布时间】:2020-07-29 18:39:00
【问题描述】:

我正在制作一个非常基本的 Flask 应用程序来在 vps 上显示 mysql 数据库的表。

我通过 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>

【问题讨论】:

    标签: python mysql flask gunicorn digital-ocean


    【解决方案1】:

    第一个问题是您在视图中直接关闭了连接:

    @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,因为这将确保您的连接和会话在每次访问路线时都得到管理。或者,您可能希望采用Flask and SQLAlchemy without the Flask-SQLAlchemy Extension 或类似方法。如果您不想为此使用 ORM,则需要考虑如何在没有全局连接的情况下建立和拆除连接。

    【讨论】:

    • 谢谢,我会研究 SQLAlchemy
    猜你喜欢
    • 1970-01-01
    • 2016-06-29
    • 2011-07-06
    • 2014-01-15
    • 2013-04-01
    • 1970-01-01
    • 2014-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多