简体   繁体   中英

Thread-safe application with sqlalchemy, postgresql

Is it possible to use sqlalchemy for threaded applications? I have an app that creates new thread tasks. Can I use session in those threads? I have read that using scoped_session in sqlalchemy might help. Should I use scoped_session or basic session with PostgreSQL? I need to create a new connection for each thread. Thank you

By default, aSession is thread specific. In contrast, a scoped_session allows you to create session objects which share state between threads.

To answer your question specifically, when your application creates a session (or connection), resources for that connection are managed by a resource pool. Each thread your application opens will open a new connection, and be independent of the others . (Unless explicitly designed otherwise.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM