Flask SQLAlchemy setup dynamic URI

前端 未结 1 1625
南旧
南旧 2021-01-01 06:12

I have a Flask app served up under WSGI where the database URI changes over time. Every two hours the URI toggles to another database. I\'m using that time to fill up one

相关标签:
1条回答
  • 2021-01-01 06:22

    You can make a custom builder for the session that will re-create the engine and scoped session when your rules dictate it. Something like

    class SessionManager(object):
    
        def __init__(self):
            self.session = None
    
        def get_session(self):
            # return existing session or make a new engine and scoped_session
    

    To make this class transparent, use Werkzeug's LocalProxy. The code that uses the sessions won't have to change at all.

    session_manager = SessionManager()
    db_session = LocalProxy(session_manager.get_session)
    
    0 讨论(0)
提交回复
热议问题