Flask-SQLAlchemy and Gevent not closing mysql connections -
i using flask-uwsgi-websockets provide websocket functionality application. use flask-sqlalchemy connect mysql database.
flask-uwsgi-websockets uses gevent manage websocket connections.
the problem having when websocket connection ended, database connection set flask-sqlalchemy keep on living.
i have tried calling db.session.close()
, db.engine.dispose()
after every websocket connection, had no effect.
calling gevent.monkey.patch_all()
@ beginning of app not make difference.
a simple representation of doing this:
from gevent.monkey import patch_all patch_all() flask import flask flask_uwsgi_websocket import geventwebsocket 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 true: msg = client.recv() # db stuff message # following part executed when connection broken, # tried removing connection, actual # connection stay open (i can see on mysql server). db.session.close() db.engine.dispose()
i have same situation. , solution me located in mysql configuration file my.cnf
:
[mysqld] interactive_timeout=180 wait_timeout=180
you must restart mysql service after save my.cnf.
if don't want restart mysql service can use sql queries:
set global interactive_timeout = 180; set global wait_timeout = 180;
see wait_timeout , interactive_timeout on mysql.com
Comments
Post a Comment