2016-11-04 78 views
1

我想實現連接與SQLAlchemy的和pymysql池:Connection對象不可調用的 - 的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 

有沒有人有什麼建議,有什麼不對?

感謝

回答

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) 
... 
+0

嗯,就是這樣。謝謝 –