どのようにのpython MySQLdbは読み取りタイムアウトを設定READ_TIMEOUT

Pythonでは、使用頻度の高いMySQLdb操作MySQLデータベースを。
実装ではMySQLdbない純粋なのpythonが、MySQL C APIライブラリーをカプセル化します_mysql

MySQLdbサポートしているかどうかread_timeout、そのマニュアルでは、このパラメータは言及しませんでした。だから、read_timeoutそれは本当に、疑いの存在可能です。これは、スタックオーバーフローが同じ尋ねた上でもあった質問を

次に、我々からMySQLdbソースライブラリのMySQLdb-Pythonのgithubのアドレスは、開始のサポートを見てくださいread_timeout

MySQLdbソース

コードのライブラリを見てくださいHISTORYファイル

beta 4
======

Added support for the MySQL read_timeout option. Contributed by
Jean Schurger ([email protected]).

Added a workaround so that the MySQL character set utf8mb4 works with Python; utf8 is substituted
on the Python side.

これは明確にパラメータがされていることを述べてきたread_timeoutサポートを提供しました。

外観は、基礎となるコードは、達成する方法である_mysql.cを

/* According to https://dev.mysql.com/doc/refman/5.1/en/mysql-options.html
   The MYSQL_OPT_READ_TIMEOUT apear in the version 5.1.12 */
#if MYSQL_VERSION_ID > 50112
#define HAVE_MYSQL_OPT_TIMEOUTS 1
#endif


#ifdef HAVE_MYSQL_OPT_TIMEOUTS
    if (read_timeout) {
        unsigned int timeout = read_timeout;
        mysql_options(&(self->connection), MYSQL_OPT_READ_TIMEOUT,
                (char *)&timeout);
    }
    if (write_timeout) {
        unsigned int timeout = write_timeout;
        mysql_options(&(self->connection), MYSQL_OPT_WRITE_TIMEOUT,
                (char *)&timeout);
    }
#endif

コードから、あなたが見ることができる、MySQLはバージョン5.1.12からサポートされていますread_timeout

MySQLdb特定の実装では、により
mysql_options()パラメータの設定MYSQL_OPT_READ_TIMEOUT、読み出し達成します。

オンMYSQL_OPT_READ_TIMEOUTMYSQL_OPT_WRITE_TIMEOUTあなたが公式のMySQLのドキュメントを参照することができ
mysql_options()

のは、見てみましょうか。MySQLdb-pythonread_timeout

read_timeout

次の例では、我々が設定read_timeout5Sとして、SQL文が実行され、5秒以上。
その実行結果を表示します。

import MySQLdb
from datetime import datetime


host = "127.0.0.1"
port = 3306
sql = "select sleep(10)"
user = "root"
passwd = "Aa123456"

conn = MySQLdb.connect(host=host, port=port, user=user,passwd=passwd, connect_timeout=2, read_timeout=5, charset="utf8")
cursor = conn.cursor()

print("now:", datetime.now())

try:
        cursor.execute(sql)
except Exception as e:
        print("now:", datetime.now())
        print("except:", e)
        raise

ret = cursor.fetchone()
print("result:", ret)

cursor.close()
conn.close()
print("end")

出力:

now: 2019-07-28 15:57:40.424942
now: 2019-07-28 15:57:45.425193
except: (2013, 'Lost connection to MySQL server during query')
Traceback (most recent call last):
  File "read_timeout.py", line 19, in <module>
    cursor.execute(sql)
  File "/Users/lanyang/workspace/orange-service/.venv/lib/python3.6/site-packages/MySQLdb/cursors.py", line 198, in execute
    res = self._query(query)
  File "/Users/lanyang/workspace/orange-service/.venv/lib/python3.6/site-packages/MySQLdb/cursors.py", line 304, in _query
    db.query(q)
  File "/Users/lanyang/workspace/orange-service/.venv/lib/python3.6/site-packages/MySQLdb/connections.py", line 217, in query
    _mysql.connection.query(self, query)
MySQLdb._exceptions.OperationalError: (2013, 'Lost connection to MySQL server during query')

SQLステートメントは接続が切断され、5秒以上実行されたときにそれを見ることができます。
これは、所望の効果に達しています。

参照

操作PythonのMySQLデータベース

MySQLdbは、Pythonのインストール

MySQLdbは、Pythonは、ドキュメントを使用します

MySQLdbは、Pythonのgithubのアドレス

MySQLのCのAPI

おすすめ

転載: www.cnblogs.com/lanyangsh/p/11259426.html