SQLAlchemy proper session handling in multi-thread applications
You should only be calling create_engine
and scoped_session
once perprocess (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 asession. calling session methods on the scoped_session will transparentlycreate a thread-local session, if needed, and forward the method call to thesession.
Another thing to be aware of is thepool_size
of the connection pool, whichis 5 by default. For many applications that's fine, but if you are creatinglots 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