You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python teradatasql单SQL请求30秒超时配置无效问题

解决teradatasql单个SQL请求超时不生效的问题

问题分析

你配置的request_timeout用了字符串类型("30"),teradatasql驱动无法正确解析这个参数,导致超时规则没触发。另外部分场景下,驱动自带的超时机制可能受服务器端配置限制,需要额外兜底方案。

修复方案

方案1:修正request_timeout参数类型

把连接字符串里的"request_timeout":"30"改成整数"request_timeout":30,驱动才能正确识别以秒为单位的超时时间。修改后的代码:

import teradatasql
import pandas as pd

con_str = f"""{{
"host": "host",
"user": "user",
"password": "password",
"logmech": "LDAP",
"request_timeout": 30
}}"""
connection = teradatasql.connect(con_str)
with connection.cursor() as cur:
    try:
        cur.execute("select a query that takes too long from tablename")
        result = pd.DataFrame(
            cur.fetchall(), columns=[desc[0] for desc in cur.description]
        )
    except teradatasql.Error as e:
        print(f"查询超时或出错: {e}")

方案2:用Python工具手动兜底超时

如果驱动自带超时仍不生效,可通过Python内置模块强制给查询任务设置超时:

Unix/Linux系统用signal模块

import teradatasql
import pandas as pd
import signal

class TimeoutError(Exception):
    pass

def timeout_handler(signum, frame):
    raise TimeoutError("查询执行超时")

con_str = f"""{{
"host": "host",
"user": "user",
"password": "password",
"logmech": "LDAP"
}}"""
connection = teradatasql.connect(con_str)

with connection.cursor() as cur:
    # 设置30秒超时
    signal.signal(signal.SIGALRM, timeout_handler)
    signal.alarm(30)
    try:
        cur.execute("select a query that takes too long from tablename")
        result = pd.DataFrame(
            cur.fetchall(), columns=[desc[0] for desc in cur.description]
        )
        # 执行成功,取消超时
        signal.alarm(0)
    except TimeoutError:
        print("查询超时,已终止")
        connection.close()
    except teradatasql.Error as e:
        print(f"查询出错: {e}")
        signal.alarm(0)

Windows系统用threading模块

import teradatasql
import pandas as pd
import threading

class TimeoutError(Exception):
    pass

def run_with_timeout(func, timeout):
    result = None
    error = None
    def target():
        nonlocal result, error
        try:
            result = func()
        except Exception as e:
            error = e
    thread = threading.Thread(target=target)
    thread.start()
    thread.join(timeout)
    if thread.is_alive():
        raise TimeoutError("查询执行超时")
    if error:
        raise error
    return result

# 使用示例
con_str = f"""{{
"host": "host",
"user": "user",
"password": "password",
"logmech": "LDAP"
}}"""
connection = teradatasql.connect(con_str)

with connection.cursor() as cur:
    def query_func():
        cur.execute("select a query that takes too long from tablename")
        return pd.DataFrame(
            cur.fetchall(), columns=[desc[0] for desc in cur.description]
        )
    try:
        result = run_with_timeout(query_func, 30)
    except TimeoutError:
        print("查询超时")
        connection.close()

注意事项

  • 方案1的request_timeout是连接级配置,该连接下所有SQL都会应用此超时规则。
  • 手动兜底方案不受驱动限制,能更可靠地控制查询执行时长。

内容的提问来源于stack exchange,提问作者h ca

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 09:22:38