Home > database >  Thread-safe application with sqlalchemy, postgresql
Thread-safe application with sqlalchemy, postgresql

Time:07-11

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

CodePudding user response:

By default, a Session 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.)

  • Related