我目前正在使用Flask-uWSGI-Websockets为我的应用程序提供websocket功能。我使用Flask-SQLAlchemy连接到我的MySQL数据库。
Flask-uWSGI-Websockets使用gevent来管理websocket连接。
我目前遇到的问题是,当websocket连接结束时,由Flask-SQLAlchemy建立的数据库连接将继续存在。
我尝试在每次websocket连接后调用db.session.close()和db.engine.dispose(),但都没有效果。
在我的应用程序开始时调用gevent.monkey.patch_all()不会有什么不同。
我正在做的一个简单表示是这样的:
from gevent.monkey import patch_all
patch_all()
from flask import Flask
from flask_uwsgi_websocket import GeventWebSocket
from flask_sqlalchemy import SQLAlchemy
app = Flask()
ws = GeventWebSocket()
db = SQLAlchemy()
db.init_app(app)
ws.init_app(app)
@ws.route('/ws')
def websocket(client):
""" handle messages """
while client.connected is True:
msg = client.recv()
# do some db stuff with the message
# The following part is executed when the connection is broken,
# i tried this for removing the connection, but the actual
# connection will stay open (i can see this on the mysql server).
db.session.close()
db.engine.dispose()发布于 2017-05-17 09:50:24
我也有同样的情况。和我的解决方案位于mysql配置文件my.cnf
[mysqld]
interactive_timeout=180
wait_timeout=180保存my.cnf后必须重启mysql服务。
如果你不想重启mysql服务,你可以使用sql查询:
SET GLOBAL interactive_timeout = 180;
SET GLOBAL wait_timeout = 180;另请参阅mysql.com上的wait_timeout和interactive_timeout
https://stackoverflow.com/questions/31047108
复制相似问题