tutorial on adding timeout functionality to the MySQLdb module in Python

Source: Internet
Author: User
Tags diff mysql in

This article mainly introduces the MySQLdb module in Python to add timeout function of the tutorial, timeout function in the server's operational dimension is very useful, need friends can refer to the

Using Python to manipulate MySQL databases often uses the MySQLdb module.

Today in the process of development found MySQLdb.connect some parameters can not be set. We can see from this page that when connect, the option that can be set and client_flags and the MySQL C API are much worse.

A very important parameter mysql_opt_read_timeout cannot set, this parameter if does not set, the extreme condition MYSQL is in hang to live, automatically switches the IP drift, the client cannot connect to the new MySQL.

To MySQLdb option is very simple, just modify _mysql.c the Python object map to the MySQL operation of the file, add parameters, and then add a section of Mysql_option.

Here is the modified git diff file

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the ? View Code BASH   diff--git a/_mysql.c b/_mysql.c index 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 +51 0,8 @@ _mysql_connectionobject_initialize (&init_command, &read_default_file, &read_default_group, & Client_flag, &ssl,-&local_infile/* Don't PATCH for reconnect, idiots + &local_infile, &read_timeout + /* Don't PATCH for reconnect, idiots IF You do this, I'll not 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), Mysql_opt_rea D_timeout, (char *) &timeout); +} + if (compress!=-1) {Mysql_options (& (Self->connection), mysql_opt_compress, 0); Client_flag |= Client_compre SS;

Code modified, Python setup.py install can be, if there are mysql_config problems found. You also need to modify the setup_posix.py file.

?

1 2 3 4 5 6 7 8 9 10 11 12 13-14 hoterran@hoterran-laptop:~/projects/mysql-python-1.2.3$ git diff setup_posix.py diff--git a/setup_posix.py B/setup_ posix.py index 86432F5. F4F08F1 100644---a/setup_posix.py +++ b/setup_posix.py @@ -23,7 +23,7 @@ def mysql_config (what): if ret/256 > 1:rais E environmenterror ('%s not found '% (Mysql_config.path,)) return Data-mysql_config.path = "Mysql_config" +MYSQL_CONFIG.P Ath = "/usr/local/mysql/bin/mysql_config" Def get_config (): Import OS, sys

The compiler passes, we try to add the Read_timeout this parameter.

?

1 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)

And then execute the statement before you try to put MySQL in the gdb Hang 10s, Python will be thrown out of the wrong

?

1 2 3 4 5 6 7 8 9 10 Operationalerror: (2013, ' 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: (2013, ' Lost connection to MySQL server during que Ry ') > <string> (1) <module> ()->none
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.