简体   繁体   English

如何在 PostgreSQL 与 SQLAlchemy 和 psycopg2 的连接上设置“lock_timeout”?

[英]How to set `lock_timeout` on a PostgreSQL connection with SQLAlchemy and psycopg2?

With PostgreSQL, you can run this query to set a specific lock_timeout for a session:使用 PostgreSQL,您可以运行此查询来为 session 设置特定的lock_timeout

SET lock_timeout TO '3s'

I'm wondering if there is a nice way to set this option when setting up a connection with SQLAlchemy.我想知道在设置与 SQLAlchemy 的连接时是否有设置此选项的好方法。 The way I'm instantiating SQLAlchemy sessions is the following:我实例化 SQLAlchemy 会话的方式如下:

engine = create_engine('postgresql+psycopg2://{user}:{pswd}@{host}:{port}/{name}')
session = scoped_session(sessionmaker(bind=engine))

I've tried passing it in connect_args but that is not supported:我试过在connect_args中传递它,但不支持:

engine = create_engine(
    'postgresql+psycopg2://{user}:{pswd}@{host}:{port}/{name}',
    connect_args={'lock_timeout': 3}
)

Is there a way to set this option per-session/connection with SQLAlchemy and psycopg2?有没有办法使用 SQLAlchemy 和 psycopg2 为每个会话/连接设置此选项?

As it turned out, this is the right way to set lock_timeout for a session (note that the value is in milliseconds):事实证明,这是为 session 设置lock_timeout的正确方法(注意该值以毫秒为单位):

engine = create_engine(
    'postgresql+psycopg2://{user}:{pswd}@{host}:{port}/{name}',
    connect_args={'options': '-c lock_timeout=3000'}
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM