我正在尝试使用SQLAlchemy和pymysql实现连接池:
import pymysql
import sqlalchemy.pool as pool
# Lifted from the pymysql docs: https://github.com/PyMySQL/PyMySQL/blob/master/example.py
cnx = pymysql.connect(host='localhost',user='xxxx',password='xxxx',db='mydatabase')
# Lifted straight from the SQLAlchemy docs http://docs.sqlalchemy.org/en/latest/core/pooling.html:
cnxPool = pool.QueuePool(cnx, max_overflow=10, pool_size=5)
conn = cnxPool.connect()
cursor = conn.cursor()
cursor.execute("select * from PUBLIC_URLS")
cursor.close()
我只是在一个相当冗长的堆栈跟踪的底部得到以下错误:
File "/usr/local/lib/python2.7/dist-packages/SQLAlchemy-1.1.3-py2.7-linux-x86_64.egg/sqlalchemy/pool.py", line 279, in <lambda> return lambda crec: creator() TypeError: 'Connection' object is not callable
有人会对什么是错误提出任何建议吗?
由于
答案 0 :(得分:1)
QueuePool
的第一个参数必须是返回新连接的函数。只要池需要新连接,就会调用它。
您正在传递连接,而不是函数。当QueuePool
尝试调用它时,会发生错误TypeError: 'Connection' object is not callable
。
请改为尝试:
...
def getconn():
return pymysql.connect(host='localhost',user='xxxx',password='xxxx',db='mydatabase')
cnxPool = pool.QueuePool(getconn, max_overflow=10, pool_size=5)
...