【发布时间】:2015-09-11 20:50:59
【问题描述】:
我目前正在使用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()
【问题讨论】:
-
您确定已达到会话结束代码吗?是否会引发异常?在这种情况下,我会将会话结束代码放在
finally语句的正文中
标签: sqlalchemy flask-sqlalchemy uwsgi gevent