sqlalchemy和asyncpg–设置postgres statement_timeout

发布于 2025-01-31 13:43:37 字数 417 浏览 4 评论 0原文

做的时候

engine: AsyncEngine = create_async_engine(...)

在这样

async with engine.connect() as conn:
    result: Result = await conn.execute(text("""..."""))

,我想指定超时。理想情况下,我能够设置仅用于此查询执行。我对Sqlalchemy进行超时并取消查询执行我也很好,但是我找不到设置的方法。

When doing

engine: AsyncEngine = create_async_engine(...)

and then

async with engine.connect() as conn:
    result: Result = await conn.execute(text("""..."""))

I would like to specify a timeout. Ideally I'd be able to set statement_timeout just for this one query execution. I am also fine with sqlalchemy doing the timeout and cancelling the query execution, but I can't find a way to set either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓬勃野心 2025-02-07 13:43:38
create_async_engine(
            db_url, connect_args={"command_timeout": 28.0}
        )

在几秒钟内使用command_timeout in connect_args

create_async_engine(
            db_url, connect_args={"command_timeout": 28.0}
        )

use command_timeout in connect_args in seconds

鹿! 2025-02-07 13:43:37

command_timeout仅在应用程序级别上设置。这意味着在应用程序中的超时错误之后,查询仍将在数据库中运行。要在数据库级别设置超时,您应该使用server_settings

create_async_engine(
    db_url, connect_args={"server_settings": {"statement_timeout": "10000"}},
)

command_timeout is set only on application level. It means query will still be running in database after timeout error in your application. To set timeout on database level you should use server_settings:

create_async_engine(
    db_url, connect_args={"server_settings": {"statement_timeout": "10000"}},
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文