0

I connect to MySql this way:

  import MySQLdb

  cnn = MySQLdb.connect(user="login", passwd="passwd", db="some db") # subject to hang
  cur = cnn.cursor()
  cur.execute("some sql") # subject to hang
  cnn.commit() # maybe subject to hang
  cur.close()

On my server theoretically, the MySql might crash or something and I want to ensure that those methods don't take infinite time to execute. I want to be able to specify a timeout or "reset" or cancel their execution.

Is there any way?

1 Answers1

2

For connecting, MySQLdb.connect() has a connect_timeout keyword argument which you can use to set a timeout on the connection. If you want to go beyond that and actually set a timeout on the methods you're calling to do things with that connection, AFAIK there is nothing that MySQLdb facilitates. You could however create a Python decorator that will take care of setting an execution timeout, as explained here: Timeout function if it takes too long to finish

Typically you won't see that a lot, but your context may require it.

Community
  • 1
  • 1
objectified
  • 252
  • 1
  • 2