SQLAlchemy session management


I am using Bokeh server with SQL datasource - data is pulled from DB and updated via bokeh callback on frontend. I use Flask with flask-sqlalchemy for accessing DB.

For now SQLAlchemy session stays open from the moment bokeh was loaded on frontend up until the page is closed (actually some time after).

It’s healthy (as I have idle - transactions in Postgres). I am willing to switch this mechanism to be “request” based (similar to how Flask is working) - each bokeh callback - a new SQLAlchemy session will be opened and terminated afterwards.

Any ideas how to do it?