小编est*_*tin的帖子

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

在我的网络应用程序中,一些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在当前事务中定义.

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

python postgresql sqlalchemy

23
推荐指数
1
解决办法
2万
查看次数

有没有办法在HG或GIT changsets上恢复下载?

我试图在客户端找到更新特殊软件(Python应用程序)的方法.客户已经有HG或GIT,我可以规定客户环境的任何要求.

但客户慢慢地打破了互联网连接.

HG,GIT和其他工具适用于具有最小流量带宽的变更集的更新过程.

但是如果变更集变大(从小版本号跳转到HEAD),下载它们可能会因连接丢失而陷入困境.

HG,GIT或其他人可以通过pull命令恢复下载吗?


使用变更集捆绑的一种方法,使用wget/curl下载它们,使用恢复下载选项下载其他变量集.然后申请捆绑.

在此解决方案中,服务器必须为每个修订跳转组合从FROM到TO(1-2,1-15,2-15)提供捆绑包.理想情况下,服务器必须通过客户端请求进行"Lazzy bundle creation".

wget -c https://repo.myserver.com/bundle?from=rev1&to=rev2
Run Code Online (Sandbox Code Playgroud)

有没有其他方法从源代码存储库更新客户端软件?

抱歉我的英语不好:(

谢谢

python git mercurial

5
推荐指数
1
解决办法
92
查看次数

Twisted DTLS连接

如何使用twcry与m2crypto(或pyopenssl)实现dtls协议?

python twisted pyopenssl m2crypto

2
推荐指数
1
解决办法
1352
查看次数

标签 统计

python ×3

git ×1

m2crypto ×1

mercurial ×1

postgresql ×1

pyopenssl ×1

sqlalchemy ×1

twisted ×1