pyodbc.connect timeout argument is ignored for calls to SQL Server

后端 未结 2 571
南方客
南方客 2021-01-04 12:55

I am using pyodbc on Linux with FreeTDS to connect to SQL Server 2005. I have noticed that the timeout argument to my connection is not being honoured by my queries.

相关标签:
2条回答
  • 2021-01-04 13:13

    Refer pyodbc connection, there are two separate timeout parameters, a variable on the Connection class (this sets the timeout for queries) and a keyword param to pyodbc.connect (and this one for the actual connection process). Based on this you are setting the timeout for the connection process in your code and not for queries.

    0 讨论(0)
  • 2021-01-04 13:32

    Add Connection.timeout variable assignment to your code. Defaults to 0 (timeout disabled), expected in seconds.

    import pyodbc
    import time
    
    connString = 'SERVER=dbserver;PORT=1433;DATABASE=db;UID=dbuser;PWD=dbpwd;' + \
                 'DRIVER=FreeTDS'
    cnxn = pyodbc.connect(connString)
    cnxn.timeout = 3
    cursor = cnxn.cursor()
    
    t1  = time.time()
    cursor.execute("SELECT MAX(Qty) FROM big_table WHERE ID<10000005")
    print cursor.fetchone()
    t2 = time.time()
    print t2-t1
    
    cursor.execute("WAITFOR DELAY '00:00:30'")
    print 'OK'
    
    0 讨论(0)
提交回复
热议问题