我无法搞清楚SQLalchemy - 我从flask-SQLalchemy切换到SQLalchemy以获得更大的灵活性 - 但如果我无法解决这个问题,我可能会完全摆脱SQLalchemy包装。
我正在使用本指南中的声明模式:http://flask.pocoo.org/docs/0.10/patterns/sqlalchemy/
#main app
from flask import Flask
from flask.ext import restful
from flask_s3 import FlaskS3
import os
from sqlalchemy import create_engine, event
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
'''
import logging
logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
'''
app = Flask(__name__)
def my_on_checkout(dbapi_conn, connection_rec, connection_proxy):
print "checkout",dbapi_conn
def my_on_checkin(dbapi_connection, connection_record):
print "checkin",dbapi_connection
#database
engine = create_engine("postgres://localhost:5432/schmoozeedb", convert_unicode=True, pool_size=20, max_overflow=0, echo=False)
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
# for just 1 load of our app, number of checkouts from the engine pool does not equal number of checkins -- are things not getting
# returned to our connection pool?
event.listen(engine, 'checkout', my_on_checkout)
event.listen(engine, 'checkin', my_on_checkin)
Base = declarative_base()
Base.query = db_session.query_property()
def init_db():
# import all modules here that might define models so that
# they will be registered properly on the metadata. Otherwise
# you will have to import them first before calling init_db()
import models
Base.metadata.create_all(bind=engine)
@app.route('/demo2/<user_email>/<zip_code>', methods=['GET', 'POST'])
def demo2(user_email=None, zip_code=None):
# do some stuff which interacts with a db then render a template
# the template starts polling the server until polling is complete
return render_template('cardangular2.html', ssId = ssId, data = rlayer.hgetall(ssId))
# this is how I am closing the sessions.
@app.teardown_appcontext
def shutdown_session(exception=None):
print 'closing session'
db_session.remove()
我有事件监听器,这是我注意到的,这是我的问题:即使在轮询完成后页面不再进行交互,连接池中有5个签出并且只有3个签到与服务器。只是fyi,/ canvaslocal2 / update只是轮询器,它在这个例子中完成了5次轮询。
checkout <connection object at 0x108c192b0; dsn: 'dbname=schmoozeedb host=localhost port=5432', closed: 0>
checkout <connection object at 0x108c19770; dsn: 'dbname=schmoozeedb host=localhost port=5432', closed: 0>
checkout <connection object at 0x108c198a0; dsn: 'dbname=schmoozeedb host=localhost port=5432', closed: 0>
checkout <connection object at 0x108c19640; dsn: 'dbname=schmoozeedb host=localhost port=5432', closed: 0>
checkin <connection object at 0x108c192b0; dsn: 'dbname=schmoozeedb host=localhost port=5432', closed: 0>
127.0.0.1 - - [17/Feb/2015 14:31:23] "GET /demo2 HTTP/1.1" 200 -
checkout <connection object at 0x108c192b0; dsn: 'dbname=schmoozeedb host=localhost port=5432', closed: 0>
checkin <connection object at 0x108c198a0; dsn: 'dbname=schmoozeedb host=localhost port=5432', closed: 0>
checkin <connection object at 0x108c19770; dsn: 'dbname=schmoozeedb host=localhost port=5432', closed: 0>
storeFeedWrapper: 0.352962970734 s
closing session
127.0.0.1 - - [17/Feb/2015 14:31:23] "POST /canvaslocal2/update HTTP/1.1" 200 -
storeFeedWrapper: 0.373705148697 s
storeFeedWrapper: 0.541649103165 s
closing session
127.0.0.1 - - [17/Feb/2015 14:31:24] "POST /canvaslocal2/update HTTP/1.1" 200 -
closing session
127.0.0.1 - - [17/Feb/2015 14:31:25] "POST /canvaslocal2/update HTTP/1.1" 200 -
storeFeedWrapper: 2.3683412075 s
closing session
127.0.0.1 - - [17/Feb/2015 14:31:26] "POST /canvaslocal2/update HTTP/1.1" 200 -
storeFeedWrapper: 3.85505199432 s
storeFeedWrapper: 4.00069713593 s
aggAllFeeds total operation: 4.00373697281 s
aggAllFeeds: 4.00382304192 s
closing session
127.0.0.1 - - [17/Feb/2015 14:31:27] "POST /canvaslocal2/update HTTP/1.1" 200 -
当我停止服务器(ctrl + C)时:
checkin <connection object at 0x108c192b0; dsn: 'dbname=schmoozeedb host=localhost port=5432', closed: 0>
checkin <connection object at 0x108c19640; dsn: 'dbname=schmoozeedb host=localhost port=5432', closed: 0>
其余的连接会自行检查。
我不是db或SQLalchemy专家 - 任何人都有任何想法?我有一个测试类,我在webapp中向/ demo2发出50个请求。而且由于这个问题出现了某种检查泄漏,我无法通过测试。
答案 0 :(得分:3)
我认为原因是:每个线程都会“检出”连接,而不是每个请求。
比方说,Flask旋转了5个线程来满足您的请求。当负载较低时,它会将线程池减少到3.此时,您将只看到两个签入。在其线程关闭之前,其余的连接将不会被检入,这会在您的应用程序退出时发生。