【问题标题】:How do I set timeouts with the Sybase module in Python?如何在 Python 中使用 Sybase 模块设置超时?
【发布时间】:2015-06-06 10:12:59
【问题描述】:

有时我会遇到查询 sybase 需要很长时间的问题。我想等待 30 秒左右,然后超时。这是我的代码:

import Sybase

db = Sybase.connect('server','name','pass','database')
c = db.cursor()
c.execute("select statement that takes a long time")
list1 = c.fetchall()

print list1

如何重写我的代码,以便当查询花费超过 30 秒时,它会生成一个空的 list1?

【问题讨论】:

  • 据我所知,没有。我会尝试通过向 python-sybase-misc@lists.sourceforge.net 发送电子邮件来询问 python-sybase 邮件列表,但我看到 a previous question 在那里询问同样的事情(几乎)没有回应。我没有看到任何文档表明任何方法都有 timeout 参数。

标签: python sybase


【解决方案1】:

我看到了两种可能性:

  1. (更难)在服务器端配置资源限制。以下是详细信息12
  2. (更简单)在单独的线程中运行您的 Python 代码,并在达到 30 秒限制后将其终止。在这里你有一个example

在您的情况下,选择 2:

def func():
   db = Sybase.connect('server','name','pass','database')
   c = db.cursor()
   c.execute("select statement that takes a long time")
   list1 = c.fetchall()
   print list1

A = KThread(target=func)
A.start()
time.sleep(30)
if not A.isAlive():
   A.kill()

GL!

【讨论】:

    猜你喜欢
    • 2012-01-30
    • 2020-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 2013-01-08
    • 2017-10-07
    相关资源
    最近更新 更多