SQLAlchemy proper session handling in multi-thread applications

后端 未结 1 866
春和景丽
春和景丽 2020-12-12 15:21

I have trouble understanding how to properly open and close database sessions efficiently, as I understood by the sqlalchemy documentation, if I use scoped_session to constr

相关标签:
1条回答
  • 2020-12-12 15:56

    You should only be calling create_engine and scoped_session once per process (per database). Each will get its own pool of connections or sessions (respectively), so you want to make sure you're only creating one pool. Just make it a module level global. if you need to manage your sessions more preciesly than that, you probably shouldn't be using scoped_session

    Another change to make is to use DBSession directly as though it were a session. calling session methods on the scoped_session will transparently create a thread-local session, if needed, and forward the method call to the session.

    Another thing to be aware of is the pool_size of the connection pool, which is 5 by default. For many applications that's fine, but if you are creating lots of threads, you might need to tune that parameter

    DATABASE_CONNECTION_INFO = 'mysql://username:password@localhost:3306/dbname'
    db_engine = create_engine(DATABASE_CONNECTION_INFO, echo=False)
    DBSession = scoped_session(
        sessionmaker(
            autoflush=True,
            autocommit=False,
            bind=db_engine
        )
    )
    
    
    class MTWorker(object):
    
        def __init__(self, worker_count=5):
            self.task_queue = Queue()
            self.worker_count = worker_count
    # snip
    
    0 讨论(0)
提交回复
热议问题