Answer the question
In order to leave comments, you need to log in
How to write a connection check code, and if the connection is closed, then try to reconnect to the database?
During the grabber operation, the server unexpectedly closed the connection with it
class DBController:
def __init__(self,database=database, user=user, password=password, host=host, port=port):
self.conn=psycopg2.connect(database=database,user=user,password=password,host=host,port=port)
self.cur=self.conn.cursor()
def reconnect(self):
self.conn=psycopg2.connect(database=database,user=user,password=password,host=host,port=port)
self.cur=self.conn.cursor()
except Exception:
# Тут должен быть цикл со проверкой статуса текущего подключения, но как его сделать?
#sleep(1)
#While status ='подключения нет' :
reconnect()
Answer the question
In order to leave comments, you need to log in
The Pythonic way is "ask forgiveness not permission".
So try. An exception fell out - process it.
The only thing is probably worth calling reconnect() a limited number of times and at least pausing between attempts.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question