gpt4 book ai didi

python - 连接池耗尽 psycopg2

转载 作者:行者123 更新时间:2023-11-28 22:16:26 26 4
gpt4 key购买 nike

我对 Postgresql psycopg2 有疑问。我收到错误:

error in executing with exception: connection pool exhausted

我的代码:

from psycopg2 import pool
import pandas.io.sql as sqlio
import pandas as pd
db = pool.ThreadedConnectionPool(5, 100,host=POSTGRES['host'],
database=POSTGRES['database'],user=POSTGRES['username'],
password=POSTGRES['password'],port=POSTGRES['port'])


try:
sql = "select * from role"
data = sqlio.read_sql_query(sql, db.getconn())
return data.to_json(orient='records')
except Exception as e:
print "error in executing with exception: ", e
return pd.DataFrame({'empty' : []})

这个请求应该只返回 5 行,但我得到了这个错误。

你知道我为什么会收到这个错误吗?

我的 Postgresql 数据库(中型实例)部署在公共(public)云上。

提前致谢

最佳答案

看起来您需要在某个时候将连接返回到池中,请参阅:

http://initd.org/psycopg/docs/pool.html#psycopg2.pool.AbstractConnectionPool.putconn

即像这样的东西:

sql = "select * from role"  
try:
conn = db.getconn()
try:
data = sqlio.read_sql_query(sql, conn)
finally:
pool.putconn(conn)
return data.to_json(orient='records')
except Exception as e:
print "error in executing with exception: ", e
return pd.DataFrame({'empty' : []})

关于python - 连接池耗尽 psycopg2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52184847/

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