解决 celery 连接mysql丢失问题 (2006, ‘MySQL server has gone away‘)

使用Django和celery开发程序时,遇到celery经常报下面错

具体错误如下:

[2023-02-17 11:41:57,060: WARNING/MainProcess] /usr/local/lib/python3.9/site-packages/celery/app/trace.py:660: RuntimeWarning: Exception raised outside body: OperationalError(2006, 'MySQL server has gone away'):
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/mysql/base.py", line 75, in execute
    return self.cursor.execute(query, args)
  File "/usr/local/lib/python3.9/site-packages/MySQLdb/cursors.py", line 206, in execute
    res = self._query(query)
  File "/usr/local/lib/python3.9/site-packages/MySQLdb/cursors.py", line 319, in _query
    db.query(q)
  File "/usr/local/lib/python3.9/site-packages/MySQLdb/connections.py", line 254, in query
    _mysql.connection.query(self, query)
MySQLdb.OperationalError: (2006, 'MySQL server has gone away')

分析:Django使用MySQL的时候,超时了,导致连接丢失

django.db.close_old_connections
# Register an event to reset transaction state and close connections past
# their lifetime.
def close_old_connections(**kwargs):
    for conn in connections.all(initialized_only=True):
        conn.close_if_unusable_or_obsolete()


signals.request_started.connect(close_old_connections)
signals.request_finished.connect(close_old_connections)

因此,我们可以根据上述,写成一个装饰器,专门用于celery内部访问数据使用

import logging
from django.db import close_old_connections

logger = logging.getLogger(__name__)

def handle_db_connections(func):
    def func_wrapper(*args, **kwargs):
        close_old_connections()
        logger.info(f'{func.__name__} run before do close old connection')
        result = func(*args, **kwargs)
        logger.info(f'{func.__name__} run after do close old connection')
        close_old_connections()

        return result

    return func_wrapper

然后在需要执行的model方法的时候,加上装饰器,如下:

@sync_to_async
@handle_db_connections
def get_emoticon_db_data(owner_id_id):
    return [DataClass.fill_attrs(Emoticon, emoticon.message_info) for emoticon in
            Message.objects.filter(owner_id_id=owner_id_id, message_type=2).all()]

这样就可以解决celery中连接mysql超时问题

你可能感兴趣的:(Django,Python学习,mysql,django,python,celery)