tutorial on adding timeouts to the MySQLdb module in Python

Source: Internet
Author: User
The MySQLdb module is often used when manipulating MySQL databases using Python.

Today in the development process found MySQLdb.connect some parameters cannot be set. With this page we can see that the option and the Client_flags and MySQL C APIs can be set at connect.

A very important parameter mysql_opt_read_timeout can not be set, this parameter if not set, extreme condition MYSQL is in hang, automatically switch IP drift, the client cannot re-connect to the new MYSQL.

To mysqldb add option is very simple, just modify _mysql.c this Python object map to the MySQL operation of the file, add parameters, plus a mysql_option.

The following is a modified git diff file

? View Code bashdiff--git a/_mysql.c b/_mysql.cindex d42cc54. 61a9b34 100644---a/_mysql.c+++ b/_mysql.c@@ -489,9 +489,10 @@ _mysql_connectionobject_initialize ("Named_pipe", "Init_                 Command "," Read_default_file "," Read_default_group "," Client_flag "," SSL ",-" local_infile ", + "Local_infile", "Read_timeout", NULL}, int connect_timeout = 0;+ int read_timeout = 0;int Compress =-1, Named_pipe = -1, local_infile = -1;char *init_command=null,*read_default_file=null,@@ -500,7 +501,7 @@ _mysql_connectionobject_ Initialize (self->converter = Null;self->open = 0;check_server_init ( -1);-if (! Pyarg_parsetupleandkeywords (args, Kwargs, "|ssssisoiiisssioi:connect", + if (! Pyarg_parsetupleandkeywords (args, Kwargs, "|ssssisoiiisssioii:connect", Kwlist,&host, &user, &PASSWD, &db,&port, &unix_socket, &conv,@@ -509,7 +510,8 @@ _mysql_connectionobject_initialize (&init_ command, &READ_DEFAULT_FILE,&READ_DEFAULT_GROUP,&CLIENT_FL.AG, &ssl,-&local_infile/* Do not PATCH for reconnect, idiots+ &local_i Nfile, &read_timeout+/* Do not PATCH for reconnect, Idiotsif "Do", I won't support YOUR PACKAGES. */)) return-1;@@ -540,6 +542,12 @@ _mysql_connectionobject_initialize (mysql_options (& (Self->connection), Mysql_opt_connect_timeout, (char *) &timeout);} + + if (read_timeout) {+ unsigned int timeout = read_timeout;+ mysql_options (& (self->connection), M Ysql_opt_read_timeout, (char *) &timeout), +}+if (compress! =-1) {Mysql_options (& (self->connection), Mysql_ opt_compress, 0); Client_flag |= client_compress;

After the code has been modified, Python setup.py install, if there is a problem mysql_config cannot find. You also want to modify the setup_posix.py file.

hoterran@hoterran-laptop:~/projects/mysql-python-1.2.3$ git diff setup_posix.pydiff--git a/setup_posix.py b/setup_ Posix.pyindex 86432f5. F4F08F1 100644---a/setup_posix.py+++ b/setup_posix.py@@ -23,7 +23,7 @@ def mysql_config (what):     if ret/256 > 1:
  raise EnvironmentError ("%s not found"% (Mysql_config.path,))   return data-mysql_config.path = "Mysql_config" + Mysql_config.path = "/usr/local/mysql/bin/mysql_config" Def Get_config ():   import OS, sys

Compile through, let's try to add the Read_timeout this parameter.

conn = MySQLdb.connect (host = Db_server,user = DB_USERNAME,PASSWD = Db_password,db = Db_name, Port=int (DB_PORT), CLIENT_FL AG = 2, read_timeout = 10)

Before executing the statement, you try to hang MySQL with GdB 10s, Python will be thrown wrong

Operationalerror: (' Lost connection to MySQL server during query ') >/home/hoterran/projects/dbaas/trunk/ dbtest.py () (>MYDB.EXECUTE_SQL) (conn, SQL) (PDB)--return-->/home/hoterran/projects/dbaas/trunk/dbtest.py ( ()->none> MYDB.EXECUTE_SQL (conn, SQL) (PDB) Operationalerror: ("Lost connection to MySQL server during Query ') > 
 
  
   
  (1)
  
   
    
   ()->none
  
   
 
  
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.