Answer the question
In order to leave comments, you need to log in
How to implement a reconnect to the database every N seconds?
Good evening.
There is this function:
def __init__(self):
''' creates a connection at the initialization moment
and also a cursor used later '''
try:
# creates a connection
self.conn = MySQLdb.connect( host = self.dbhost,
port = self.dbport,
user = self.dbuser,
passwd = self.dbpass
)
self.cursor = self.conn.cursor()
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)
Answer the question
In order to leave comments, you need to log in
What is the problem? Loop the code
instead
of
exit
according
to
the
established
connection
success
condition
Something like this:
def __init__(self):
while True:
''' creates a connection at the initialization moment
and also a cursor used later '''
try:
# creates a connection
self.conn = MySQLdb.connect( host = self.dbhost,
port = self.dbport,
user = self.dbuser,
passwd = self.dbpass
)
self.cursor = self.conn.cursor()
break
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
time.sleep(5)
How about a bike?
docs.sqlalchemy.org/en/latest/core/pooling.html or
dev.mysql.com/doc/connector-python/en/connector-py... what's wrong?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question