A
A
apiwi2021-09-24 21:57:31
MySQL
apiwi, 2021-09-24 21:57:31

How to fix WinError 10054 pyMySql error?

There is a telegram bot with a database that gets the number of users in the database and performs operations on each of them.
The task of the function is to send notifications to users with certain criteria
. The database is on a paid hosting
. Every 10 minutes, the following error pops up:

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] Удаленный хост принудительно разорвал существующее подключение)')


I close all connections, nothing helps. Muscle of the latest version
Here is a piece of code:
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()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
apiwi, 2021-09-25
@apiwi

The problem was in the pool
bot.polling(none_stop=True, interval=0, timeout=20)

L
Lazy @BojackHorseman MySQL, 2021-09-24
Tag

the answer is easily found by a search engine

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question