SQLalchemy, Flask, Python, connections that do not return to the pool

I'm having trouble calculating SQLalchemy - I switched from the SQLalchemy flag to SQLalchemy for more flexibility, but I can just get rid of the SQLalchemy wrapper at all if I can't figure it out.

I use the declarative template from this guide: 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()

Magazines

from initapp.py I have event listeners, and here is what I notice, and here is my problem: there are 5 checks from the connection pool and only 3 sessions even after the poll is completed, and the page no longer interacts with the server. Just fyi, / canvaslocal2 / update is just a poller, it ended after 5 polls in this case.

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 -

Conclusion

When I stop my server (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 - - ? , 50 /demo 2 webapp. - , - , .

+4
1

, : "" , .

, , Flask 5 . , 3. . , , , .

+2

All Articles