[英]Maintaining MySQL connection with peewee in a Flask web app
I have a simple Flask web app that uses peewee to manage the MySQL connection. 我有一个简单的Flask Web应用程序,该应用程序使用peewee来管理MySQL连接。 Unfortunately it seems I missed something important from my code, therefore I'm getting an
pymysql.err.OperationalError: (2006, "MySQL server has gone away..
error after 10-20 minutes of using the site. If I restart the app it works fine again, so I assume I handle the connection/connections wrong. 不幸的是,我似乎错过了代码中的一些重要内容,因此我遇到了
pymysql.err.OperationalError: (2006, "MySQL server has gone away..
在使用网站10-20分钟后出现错误。如果重新启动应用程序,它再次正常工作,所以我认为我处理错误的连接。
I have some basic queries that I'm using to display lists on my UI. 我有一些用于在UI上显示列表的基本查询。 Since I'm new to Python it's possible that my whole logic is wrong, so I would be really happy if somebody could me explain what is the right way to manage (connect-close connection) queries with peewee in my case (simple website without any user functions).
由于我是Python的新手,所以我的整个逻辑很可能是错误的,因此如果有人可以解释在我的情况下使用peewee管理(连接-关闭连接)查询的正确方法,我将非常高兴(简单网站没有任何用户功能)。
You can see on the below code how I'm doing it now. 您可以在下面的代码中看到我现在的工作方式。 It's fine until I connect, but after I connected to the db I'm just calling the queries without dealing the connection.
直到连接才好,但是连接到数据库后,我只是在不处理连接的情况下调用查询。 I assume I should close the connection based on the query that has been called, but couldn't find a proper way.
我假设我应该根据已调用的查询关闭连接,但找不到正确的方法。 Calling the myDB.close() isn't enough or I'm using it in wrong.
仅仅调用myDB.close()是不够的,否则我使用的是错误的。
# this is how I connect to the MySQL
import peewee as pw
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
myDB = pw.MySQLDatabase("", host="", user="", passwd="")
class MySQLModel(pw.Model):
"""A base model that will use our MySQL database"""
class Meta:
database = myDB
class city(MySQLModel):
...
class building(MySQLModel):
...
myDB.connect()
# this is how I manage the homepage
cityList = []
cityList = city.select().order_by(city.objectId).limit(15)
buildings = []
buildings = building.select().order_by(building.buildingName).limit(180)
def getSearchResult (content_from_url):
searchResultList = []
searchResultList = city.select().where(city.objTitle.contains(content_from_url))
return searchResultList
@app.route('/', methods=['GET', 'POST'])
def main_page():
search = request.args.get('search')
if search:
return render_template("results.html", search = getSearchResult(search), str = search)
else:
return render_template("home.html", name=cityList, buildList=buildings)
# and this is how the subpage
relatedCityList = []
slugObj = []
buildings2 = []
buildings2 = building.select().order_by(building.buildingName).limit(180)
def getLink (title_for_link):
slugObj = city.get(city.urlSlug == title_for_link)
return slugObj
def displayRelatedCity (city_to_filter):
resultCity = city.get(city.urlSlug == city_to_filter)
relatedCityList = city.select().where(city.objTitle == resultCity.objTitle).limit(20)
return relatedCityList
@app.route('/city-list/<content>', methods=['GET', 'POST'])
def city_page(content):
linkText = getLink(content)
return render_template("details.html", relatedCity = displayRelatedCity(content), buildingName = linkText.buildingName, buildz = buildings2)
myDB.close()
You need to be reconnecting on request/response. 您需要根据请求/响应重新连接。 http://docs.peewee-orm.com/en/latest/peewee/database.html#adding-request-hooks
http://docs.peewee-orm.com/en/latest/peewee/database.html#adding-request-hooks
There's even a section on Flask. 甚至有关于Flask的部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.