Juan Bravo
Juan Bravo

Reputation: 1

Python MySQL .cursor() gets stuck when connection is down

When I try to retrieve a cursor from a MySQL Connection using .cursor() from mysql.connector library for Python3 while the connection is not up (Internet down, for example) it gets completely stuck and hangs for around 16 minutes, before even throwing an exception. Any ideas of what can be done to reduce this delay?

def retrieveCursor(mysqlconnection):

  printf("Program started")
  try:
    printf("Going to retrieve the cursor")
    cursor=mysqlconnection.cursor()
  except:
    printf("Exception raised while retreiving the cursor")
    return -1
  else:
    printf("MySQL cursor retrieved")
    return cursor

If Internet is up, the output would be:

Program started
Going to retrieve the cursor
MySQL cursor retrieved

If Internet is down (but was up when the connection was originally established):

Program started
Going to retrieve the cursor
.. nothing for 16 minutes ..
Exception raised while retreiving the cursor 

Any ideas of why 16 minutes and how to reduce it? Thanks!!

Upvotes: 0

Views: 345

Answers (1)

DeepSpace
DeepSpace

Reputation: 81604

One of the arguments that can be passed to connection __init__ is connection_timeout that changes the default. See the docs.

Upvotes: 1

Related Questions