Art*_*sky 3 python postgresql sqlalchemy psycopg2
使用 PostgreSQL,您可以运行此查询来设置特定的lock_timeout会话:
SET lock_timeout TO '3s'
Run Code Online (Sandbox Code Playgroud)
我想知道在设置与 SQLAlchemy 的连接时是否有一个很好的方法来设置此选项。我实例化 SQLAlchemy 会话的方式如下:
engine = create_engine('postgresql+psycopg2://{user}:{pswd}@{host}:{port}/{name}')
session = scoped_session(sessionmaker(bind=engine))
Run Code Online (Sandbox Code Playgroud)
我尝试过传递它,connect_args但不支持:
engine = create_engine(
'postgresql+psycopg2://{user}:{pswd}@{host}:{port}/{name}',
connect_args={'lock_timeout': 3}
)
Run Code Online (Sandbox Code Playgroud)
有没有办法使用 SQLAlchemy 和 psycopg2 设置每个会话/连接的此选项?
事实证明,这是设置lock_timeout会话的正确方法(请注意,该值以毫秒为单位):
engine = create_engine(
'postgresql+psycopg2://{user}:{pswd}@{host}:{port}/{name}',
connect_args={'options': '-c lock_timeout=3000'}
)
Run Code Online (Sandbox Code Playgroud)