Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQLdb initial connection timeout

I'm using MySQLdb in python to connect to a remote database and execute a query. So far so good. However, it's possible that, for any number of reasons, there simply isn't a MySQL server (or indeed any server) at the designated IP address. In these cases, at the moment MySQLdb hangs indefinitely. This isn't ideal, I'd prefer to be able to set a timeout which if exceeded would allow me to raise and exception and get on with my life.

Is there a simple way to do this?

like image 405
TimD Avatar asked Sep 03 '13 23:09

TimD


1 Answers

You can pass connect_timeout argument to MySQLdb.connect function. Quote from docs:

connect_timeout

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

Default: no timeout (?)

like image 74
alecxe Avatar answered Oct 12 '22 14:10

alecxe