TimD
TimD

Reputation: 1381

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?

Upvotes: 8

Views: 12678

Answers (1)

alecxe
alecxe

Reputation: 473763

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

Upvotes: 12

Related Questions