我正在尝试将烧瓶使用sqlalchemy(无flask-sqlalchemy扩展名)。我正在努力将sqlalchemy的会话对象与flask集成在一起。
我看过几种方法:
1) Create a new session everytime I want to query the database and close the corresponding session at the end of the request(in the request function itself rather than taking help from Flask). This does not scale well as it exhausts the sqlalchemy connection pool quickly with the number of increasing requests.
2) Use the scoped session, query the database and remove the scoped session acquired at the end of request using the flask's app.after_request decorator. This approach works, but I am confused so as how to make use of it, as some places recommend to use the Flask's global request variable g to keep track of which session is allocated to which request while others do not use it at all.
我决定使用第二个选项,因为对于大型应用程序,第一个选项根本无法扩展。 我想知道第二种方法是否有任何陷阱,例如使用g运算符或不使用g运算符的并发会话处理相同的对象。另外,不使用g变量对sqlalchemy scoped_session没有任何影响吗?
任何帮助将不胜感激。