espero que possa ajudar:
while True: #it works until the data was not saved
try:
with connection.cursor() as cursor:
print ('saving company details to db')
cursor.execute("""INSERT INTO company(
companyname,address,city,pincode,website)
VALUES (%s, %s, %s, %s, %s)""",
(companyname, companyaddress, companycity,
companypostalcode, companywebsite))
connection.commit()
break
except OperationalError:
connection.ping(True)
print ("Company Data saved")
Você também pode ver uma pergunta semelhante com o uso do pool de conexões
ou leia a fonte