scoped_session(sessionmaker()) or plain sessionmaker() in sqlalchemy?

前端 未结 4 734
南笙
南笙 2021-01-31 08:12

I am using SQlAlchemy in my web project. What should I use - scoped_session(sessionmaker()) or plain sessionmaker() - and why? Or should I use somethin

4条回答
  •  梦毁少年i
    2021-01-31 08:39

    Reading the documentation is recommended:

    the scoped_session() function is provided which produces a thread-managed registry of Session objects. It is commonly used in web applications so that a single global variable can be used to safely represent transactional sessions with sets of objects, localized to a single thread.

    In short, use scoped_session() for thread safety.

提交回复
热议问题