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

前端 未结 4 735
南笙
南笙 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:26

    I am looking into this myself, but I am not an expert.

    My three points are:

    1. SQLAlchemy docs provide a proposed approach using scoped_session, per Mr. Kluev's comment above, at this link: http://docs.sqlalchemy.org/en/rel_0_9/orm/session.html#using-thread-local-scope-with-web-applications.
    2. At that web location, the SQLAlchemy docs also say that it is "...strongly recommended that the integration tools provided with the web framework itself be used, if available, instead of scoped_session."
    3. Flask-SQLAlchemy, for example, appears to claim that it takes care of this: http://pythonhosted.org/Flask-SQLAlchemy/quickstart.html#a-minimal-application

提交回复
热议问题