如何为查询执行设置语句超时

est*_*tin 23 python postgresql sqlalchemy

在我的网络应用程序中,一些postgres sql查询需要时间来执行.我想只为其中一部分设置语句超时.

查询的一部分必须通过超时取消,但其他部分必须无任何限制地工作.

在postgres中存在statement_timeout函数.

如何用statement_timeout函数包装SqlAlchemy查询?

像这样:

SET statement_timeout TO 1000; -- timeout for one second
<sqlalchemy generated query>;
RESET statement_timeout; -- reset
Run Code Online (Sandbox Code Playgroud)

对我来说完美的方式设置查询超时,如下所示:

users = session.query(User).timeout(0.5).all()
Run Code Online (Sandbox Code Playgroud)

SqlAlchemy必须:1)设置语句超时2)执行查询并返回结果3)当前会话的重置语句超时

可能是为查询执行设置超时的其他方法?

更新1.我的解决方案

我的解决方案是自定义连接代理(使用psycopg2 == 2.4和SQLAlchemy == 0.6.6测试):

from sqlalchemy.interfaces import ConnectionProxy

class TimeOutProxy(ConnectionProxy):
    def cursor_execute(self, execute, cursor, statement, parameters, context, executemany):

        timeout = context.execution_options.get('timeout', None)

        if timeout:
            c = cursor._parent.cursor()
            c.execute('SET statement_timeout TO %d;' % int(timeout * 1000))
            c.close()

        return execute(cursor, statement, parameters, context)


engine = create_engine(URL, proxy=TimeOutProxy(), pool_size=1, max_overflow=0)
Run Code Online (Sandbox Code Playgroud)

此解决方案无需重置statement_timeout,因为每个SqlAlchemy查询都在隔离事务中执行,而statement_timeout在当前事务中定义.

用法示例(以秒为单位的超时参数):

Session.query(Author).execution_options(timeout=0.001).all()

Session.bind.execute(text('select * from author;') \
      .execution_options(timeout=0.001)) \
      .fetchall()
Run Code Online (Sandbox Code Playgroud)

小智 3

您应该查看 SQLAlchemy <= 0.6 提供的扩展:

http://www.sqlalchemy.org/docs/06/orm/interfaces.html

您可以通过一些钩子将单个操作插入代码中。

SQLAlchemy 0.7+ 现在有一个事件系统......可能有类似的东西。看

http://www.sqlalchemy.org/docs/core/events.html