Limiting the number of Session objects created in sqlalchemy

Kartik Source

I am trying to find out how we can limit the number of sessions or if there is a way to set up a maximum number of resources in SQL Alchemy session pool. I am working with a legacy codebase that initialises SessionPool as follows. I am not sure if this is important, but we use Flask in our applications.

  from sql import config as sqla_config

  if Session.registry.has():
    db_session = Session()

  if db_session is None:
     engine = sqla_config.get_engine("my-db", "primary", role=None)
     db_session = Session(bind=engine)

  return db_session 

I want to limit the number of sessions that can be created. How can I do that? This is a legacy code base so I am limited in the kind of changes I can make. I am aware of QueuedPool, but I don't know how to use it in this context.



answered 5 months ago Sraw #1

According to source code. You can set the following variable in flask config to achieve what you want:


comments powered by Disqus