Python NoneType и соединение потеряно в Mysql пуле - PullRequest
0 голосов
/ 15 января 2020

Я получаю следующую ошибку:

2055: Lost connection to MySQL server at 'localhost:3306', system error: 104 Connection reset by peer
    Traceback (most recent call last):
      File "script.py", line 183, in <module>
        p1 = MyScript()
      File "script.py", line 77, in _init_
        if connection_object.is_connected():
      File "/usr/local/lib/python3.7/dist-packages/mysql/connector/pooling.py", line 103, in _getattr_
        return getattr(self._cnx, attr)
    AttributeError: 'NoneType' object has no attribute 'is_connected

Мой код:

def init_db_pool(self):
    self.connection_pool = mysql.connector.pooling.MySQLConnectionPool(pool_name="db_pool",
                                                                       pool_size=self.cputhreads * 2,
                                                                       pool_reset_session=True,
                                                                       host=self.db['host'],
                                                                       database=self.db['carsdatabase'],
                                                                       user=self.db['userdb'],
                                                                       password=self.db['passwddb'])

def __init__(self):
    threads = []
    self.cputhreads = os.cpu_count()
    if self.cputhreads < 4:
        self.cputhreads = 4
    try:
        self.init_db_pool()
        connection_object = self.connection_pool.get_connection()
        if connection_object.is_connected():
            records_count_sql = "Select count(*) from " + self.db['table']
            cursor = connection_object.cursor()
            cursor.execute(records_count_sql)
            record = cursor.fetchone()
            record_totals = record[0]
            records_chunks = int(record_totals / self.cputhreads)
            cursor.close()
            connection_object.close()
            for i in range(self.cputhreads):
                threads.append(Thread(target=self.is_available, args=(i, self.connection_pool, records_chunks)))
            for thread in threads:
                thread.start()

            for thread in threads:
                thread.join()
        else:
            print('Not connected to DB')
    except Error as e:
        print("Error while connecting to MySQL using Connection pool ", e) if self.debug else False
    finally:
        # closing database connection.
        if connection_object.is_connected():
            connection_object.close()

Исключение возникает потому, что последнее, если connection_object не является классом, но кажется, что основная проблема что я потерял связь с БД.

Есть идеи, почему это происходит?

Чем

...