Есть телеграмм бот с базой данных, получает количество пользователей в базе и делает операции над каждым из них.
Задача функции разослать уведомления юсерам с определенными критериями
База данных стоит на платном хостинге
Каждые 10 минут выскакивает такая ошибка:
Traceback (most recent call last):
File "d:\Python\TG\InvestBot\golibaks.py", line 47, in notified
cursor.execute("SELECT COUNT(*) FROM old_users WHERE status = '+' AND notified = '-'")
File "C:\Users\apiwi\AppData\Local\Programs\Python\Python39\lib\site-packages\pymysql\cursors.py", line 148, in execute
result = self._query(query)
File "C:\Users\apiwi\AppData\Local\Programs\Python\Python39\lib\site-packages\pymysql\cursors.py", line 310, in _query
conn.query(q)
File "C:\Users\apiwi\AppData\Local\Programs\Python\Python39\lib\site-packages\pymysql\connections.py", line 548, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "C:\Users\apiwi\AppData\Local\Programs\Python\Python39\lib\site-packages\pymysql\connections.py", line 775, in _read_query_result
result.read()
File "C:\Users\apiwi\AppData\Local\Programs\Python\Python39\lib\site-packages\pymysql\connections.py", line 1156, in read
first_packet = self.connection._read_packet()
File "C:\Users\apiwi\AppData\Local\Programs\Python\Python39\lib\site-packages\pymysql\connections.py", line 692, in _read_packet
packet_header = self._read_bytes(4)
File "C:\Users\apiwi\AppData\Local\Programs\Python\Python39\lib\site-packages\pymysql\connections.py", line 738, in _read_bytes
raise err.OperationalError(
pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query ([WinError 10054] Удаленный хост принудительно разорвал существующее подключение)')
Конекты все закрываю, ничем не помогает. Мускул последней версии
Вот кусок кода:
def notified():
threading.Timer(500, notified).start()
try:
if config.allow_kick == False: print("Kick disallowed")
else: print("Kick allowed")
with connection.cursor() as cursor:
cursor.execute("SELECT COUNT(*) FROM old_users WHERE status = '+' AND notified = '-'")
summ = list(cursor)[0]["COUNT(*)"]
count = 0
connection.commit()
while summ != count:
with connection.cursor() as cursor:
cursor.execute("SELECT * FROM old_users WHERE status = '+' AND notified = '-'")
for row in cursor:
user_id = row['user_id']
end_date = row['end_date']
now_date = datetime.datetime.now()
#print(f"{user_id} {now_date} {end_date}")
if end_date == "0000-00-00 00:00:00":
pass
else:
if now_date >= (end_date - timedelta(config.day_notify)):
cursor.execute(f"UPDATE old_users SET notified = '+' WHERE user_id = {user_id}")
bot.send_message(user_id, txs.sub_ender, reply_markup=kb.payment3)
connection.commit()
print(f"{user_id} (old) notified")
else:
pass
count = count+1
connection.commit()