连接对象不可调用 - SQLAlchemy / pymysql

Connection object not callable - SQLAlchemy / pymysql

我正在尝试使用 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

有没有人对问题有什么建议?

谢谢

QueuePool 的第一个参数必须是 returns 新连接的函数。每当池需要新连接时都会调用它。

您传递的是一个连接,而不是一个函数。当 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)
...