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

前端 未结 4 743
南笙
南笙 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条回答
  •  长情又很酷
    2021-01-31 08:52

    Scoped_session at every method will give you a thread of local session which you cannot obtain beforehand (like at the module level).It's not needed to open a new session in every method, You can use a global session , Create a session only when the global session is not available. i.e you can write a method which returns a session and add it to the init.py inside your package.

提交回复
热议问题