简体   繁体   中英

how to set time limit to connect mysql database

I am using mysql database to store data. I want if the database is available then it will store the data otherwise it will do other jobs and exclude database storage. My moto is to wait for 3 seconds for database connection. if database is not available then it will exit from connect command.

import MySQLdb
try: 
    db=MySQLdb.connect("IP for remote computer","admin","pass","database")
    *** do something****
except:
     print "database not connected"
 **** do other jobs***

now if the host ip is connected to LAN and there is no mysql on that computer it takes longer time to exit from connect command. how could i assign a 3 sec wait for the connection? after that if host ip is not connected or there is no database or mysql installed. the program will exit from MySQLdb.connect() and print database not connected.

You can pass connect_timeout param into MySQLdb.connect() .

Here is docs fragment:

connect_timeout

Abort if connect is not completed within given number of seconds.

Default: no timeout (?)

http://mysql-python.sourceforge.net/MySQLdb.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM