SQLalchemy、Flask、Python、连接未返回到池中

SQLalchemy, Flask, Python, Connections not being returned to the pool

我在弄清楚 SQLalchemy 时遇到了麻烦——我从 flask-SQLalchemy 切换到 SQLalchemy 以获得更大的灵活性——但如果我弄不明白,我可能会完全摆脱 SQLalchemy 包装器。

我正在使用本指南中的声明式模式:http://flask.pocoo.org/docs/0.10/patterns/sqlalchemy/

Initapp.py

    #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)

Webapp.py

@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()

日志

来自 initapp.py 我有事件侦听器,这是我注意到的,这是我的问题:连接池有 5 次签出,即使在轮询完成和页面后也只有 3 次签入不再与服务器交互。仅供参考,/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>

其余连接重新检查。

我不是数据库或 SQLalchemy 专家——有人知道吗?我有一个测试 class,我在 webapp 中向 /demo2 发出了 50 个请求。并且由于这个问题存在某种checkin泄漏,我无法通过测试。

我相信原因是这样的:一个连接是 "checked out" 每个线程,而不是每个请求。

举例来说,Flask 启动了 5 个线程来满足您的请求。当负载较低时,它会将线程池减少到 3。此时,您只会看到两次签入。其余连接在它们的线程关闭之前不会被重新签入,这在您的应用程序退出时发生。