1
我使用sqlalchemy的聲明性映射系統添加了一個新的ORM類。我的代碼庫有一個現有的psycopg2連接池,我想重用 - 我不希望使用我的orm類的代碼擁有自己的池。有很多現有代碼在psycopg2池上直接調用get_conn
,所以我不想只替換它。使用現有的psycopg2連接池創建一個sqlalchemy引擎
我有一個構建引擎連接的問題。
pool_config = {...}
POOL = psycopg2.pool.ThreadedConnectionPool(0, 32, **pool_config)
[...]
engine = sqlalchemy.create_engine('postgresql://', pool=POOL)
Session = sqlalchemy.orm.sessionmaker(bind=engine)
...
問題是我的電話create_engine
;
File "/home/ubuntu/environment/local/lib/python2.7/site-packages/sqlalchemy/engine/__init__.py", line 362, in create_engine
return strategy.create(*args, **kwargs)
File "/home/ubuntu/environment/local/lib/python2.7/site-packages/sqlalchemy/engine/strategies.py", line 159, in create
event.listen(pool, 'first_connect', on_connect)
File "/home/ubuntu/environment/local/lib/python2.7/site-packages/sqlalchemy/event/api.py", line 63, in listen
_event_key(target, identifier, fn).listen(*args, **kw)
File "/home/ubuntu/environment/local/lib/python2.7/site-packages/sqlalchemy/event/registry.py", line 190, in listen
dispatch_descriptor = getattr(target.dispatch, identifier)
AttributeError: 'ThreadedConnectionPool' object has no attribute 'dispatch'
是否有可能以這種方式使用我的現有池,或者是否需要創建一個單獨的連接池供這些類使用?