gpt4 book ai didi

python - 使用 sqlalchemy create_engine 配置查询/命令超时?

转载 作者:太空狗 更新时间:2023-10-30 02:02:02 26 4
gpt4 key购买 nike

以下 Python 代码片段说明了这个问题:

print("starting")

# I am trying to configure a query/command timeout of one second.
# sqlalchemy docs suggest execution_options but the documented list of options doesn't include a timeout:
# http://docs.sqlalchemy.org/en/latest/core/connections.html#sqlalchemy.engine.Connection.execution_options
# Below, I am guessing at several likely timeout parameter names:
db_engine = create_engine("postgresql://user:pass@server/catalog",
execution_options={"timeout": 1.0,
"statement_timeout": 1.0,
"query_timeout": 1.0,
"execution_timeout": 1.0})

with db_engine.connect() as db_connection:
print("got db_connection")

# Artificially force a two second query time with pg_sleep.
# This is designed to guarantee timeout conditions and trigger an exception.
result_proxy = db_connection.execute("SELECT pg_sleep(2);")

# If the timeout worked, this statement will not execute.
# Currently, it does execute, which means my timeout isn't working.
print("Query successfully complete. Got result_proxy")

最佳答案

您可以设置配置值,如 statement_timeout通过 options parameter in libpq .作为 connect 的一部分,您可以在 psycopg2 中访问此参数称呼。您可以通过 connect_args 将附加参数传递给来自 SQLAlchemy 的 connect 调用范围。所以,把它们放在一起:

engine = create_engine(..., connect_args={"options": "-c statement_timeout=1000"})

关于python - 使用 sqlalchemy create_engine 配置查询/命令超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44936527/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com