gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:02:36 25 4
gpt4 key购买 nike

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

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

初始化应用程序.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)

网络应用程序.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 次 checkout ,即使在轮询完成且页面不再交互后也只有 3 次 checkin 与服务器。仅供引用,/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 专家——有人知道吗?我有一个测试类,我在 webapp 中向/demo2 发出 50 个请求。由于这个问题存在某种 checkin 泄漏,我无法通过测试。

最佳答案

我相信原因是这样的:连接是为每个线程“ check out ”的,而不是为每个请求“ check out ”。

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

关于python - SQLalchemy、Flask、Python、连接未返回到池中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28572587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com