How to add the timeout function to the MySQLdb module in Python

Source: Internet
Author: User

How to add the timeout function to the MySQLdb module in Python

This article describes how to add the timeout function to the MySQLdb module in Python. The timeout function is very useful in server O & M. For more information, see

When using Python to operate MySQL databases, the module MySQLdb is often used.

During the development process today, some parameters of MySQLdb. connect cannot be set. On this page, we can see that the options and client_flags that can be set during connect are much worse than those of MySQL c APIs.

A very important parameter, MYSQL_OPT_READ_TIMEOUT, cannot be set. If this parameter is not set, MySQL stays in the hang state, automatically switches the IP address drift, and the client cannot re-connect to the new MySQL.

Adding Option to MySQLdb is simple. You only need to modify _ mysql. c to map the Python object to the MySQL operation file, add parameters, and add a piece of mysql_option.

The modified git diff file is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

? 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,

@-, 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_flag, & ssl,

-& Local_infile/* do not patch for reconnect, IDIOTS

+ & Local_infile, & read_timeout

+/* Do not patch for reconnect, IDIOTS

If you do this, I WILL 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_READ_TIMEOUT, (char *) & timeout );

+}

+

If (compress! =-1 ){

Mysql_options (& (self-> connection), MYSQL_OPT_COMPRESS, 0 );

Client_flag | = CLIENT_COMPRESS;

After the code is modified, run python setup. py install. If mysql_config cannot be 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:

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

After compilation is passed, let's try the read_timeout parameter added.

?

1

Conn = MySQLdb. connect (host = DB_SERVER, user = DB_USERNAME, passwd = DB_PASSWORD, db = DB_NAME, port = int (DB_PORT), client_flag = 2, read_timeout = 10)

Before executing the statement, you try to put mysql in gdb hang for 10 s, and then python will throw an exception

?

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 (18 )()

> Mydb.exe cute_ SQL (conn, SQL)

(Pdb)

-- Return --

>/Home/hoterran/Projects/dbaas/trunk/dbtest. py (18) ()-> None

> Mydb.exe cute_ SQL (conn, SQL)

(Pdb)

OperationalError: (2013, 'lost connection to MySQL server during query ')

> <String> (1) <module> ()-> 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.