apscheduler在查询期间丢失了与MySQL服务器的连接

时间:2016-03-08 16:07:10

标签: python mysql apscheduler

我使用apscheduler来执行常规作业,但我遇到了一些错误。

"查询期间与MySQL服务器的连接丢失"

为了找到答案,我尝试对它进行一些测试,并发现我的数据库(MySQL)" wait_timeout"小于调度间隔时间然后发生此错误。 (对不起,我犯了一些错误......不到......)

在测试中:

  • 我的工作设置

    scheduler.add_job(period_job,' interval',minutes = 5,id =' my_job_id')

  • 我的数据库设置

    wait_timeout = 60

  • 我的测试代码

    from apscheduler.schedulers.background import BackgroundScheduler
    from flask import Flask
    app = Flask(__name__)
    
    scheduler = BackgroundScheduler({'apscheduler.jobstores.default': {
            'type': 'sqlalchemy',
            'url': 'mysql+pymysql://user:pass@url:3306/test_apscheduler?charset=utf8'
        },
            'apscheduler.executors.default': {
                'class': 'apscheduler.executors.pool:ThreadPoolExecutor',
                'max_workers': '20'
        },
            'apscheduler.executors.processpool': {
            'type': 'processpool',
            'max_workers': '5'
        },
            'apscheduler.job_defaults.coalesce': 'false',
            'apscheduler.job_defaults.max_instances': '3',
            'apscheduler.timezone': 'UTC',
        })
    
    scheduler.start()
    
    
    @app.route('/')
    def hello_world():
        scheduler.add_job(period_job, 'interval', minutes=5, id='my_job_id')
        return 'Hello World!'
    
    
    def period_job():
        print("hihi")
    
    
    if __name__ == '__main__':
        app.run()
    

总错误信息:

Exception in thread APScheduler:
Traceback (most recent call last):
  File "C:\Users\skuo\apshcduler\lib\site-packages\sqlalchemy\engine\base.py", line 1139, in _execute_context context)
  File "C:\Users\skuo\apshcduler\lib\site-packages\sqlalchemy\engine\default.py", line 450, in do_execute cursor.execute(statement, parameters)
  File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\cursors.py", line 158, in execute result = self._query(query)
  File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\cursors.py", line 308, in _query conn.query(q)
  File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\connections.py", line 820, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\connections.py", line 1002, in _read_query_result result.read()
  File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\connections.py", line 1285, in read first_packet = self.connection._read_packet()
  File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\connections.py", line 946, in _read_packet packet_header = self._read_bytes(4)
  File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\connections.py", line 982, in _read_bytes 2013, "Lost connection to MySQL server during query")
pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Python34\lib\threading.py", line 921, in _bootstrap_inner self.run()
  File "C:\Python34\lib\threading.py", line 869, in run self._target(*self._args, **self._kwargs)
  File "C:\Users\skuo\apshcduler\lib\site-packages\apscheduler\schedulers\blocking.py", line 27, in _main_loop wait_seconds = self._process_jobs()
  File "C:\Users\skuo\apshcduler\lib\site-packages\apscheduler\schedulers\base.py", line 801, in _process_jobs for job in jobstore.get_due_jobs(now):
  File "C:\Users\skuo\apshcduler\lib\site-packages\apscheduler\jobstores\sqlalchemy.py", line 69, in get_due_jobs return self._get_jobs(self.jobs_t.c.next_run_time <= timestamp)
  File "C:\Users\skuo\apshcduler\lib\site-packages\apscheduler\jobstores\sqlalchemy.py", line 131, in _get_jobs for row in self.engine.execute(selectable):
  File "C:\Users\skuo\apshcduler\lib\site-packages\sqlalchemy\engine\base.py", line 1991, in execute return connection.execute(statement, *multiparams, **params)
  File "C:\Users\skuo\apshcduler\lib\site-packages\sqlalchemy\engine\base.py", line 914, in execute return meth(self, multiparams, params)
  File "C:\Users\skuo\apshcduler\lib\site-packages\sqlalchemy\sql\elements.py", line 323, in _execute_on_connection return connection._execute_clauseelement(self, multiparams, params)
  File "C:\Users\skuo\apshcduler\lib\site-packages\sqlalchemy\engine\base.py", line 1010, in _execute_clauseelement compiled_sql, distilled_params
  File "C:\Users\skuo\apshcduler\lib\site-packages\sqlalchemy\engine\base.py", line 1146, in _execute_context context)
  File "C:\Users\skuo\apshcduler\lib\site-packages\sqlalchemy\engine\base.py", line 1341, in _handle_dbapi_exception exc_info
  File "C:\Users\skuo\apshcduler\lib\site-packages\sqlalchemy\util\compat.py", line 200, in raise_from_cause reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "C:\Users\skuo\apshcduler\lib\site-packages\sqlalchemy\util\compat.py", line 183, in reraise raise value.with_traceback(tb)
  File "C:\Users\skuo\apshcduler\lib\site-packages\sqlalchemy\engine\base.py", line 1139, in _execute_context context)
  File "C:\Users\skuo\apshcduler\lib\site-packages\sqlalchemy\engine\default.py", line 450, in do_execute cursor.execute(statement, parameters)
  File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\cursors.py", line 158, in execute result = self._query(query)
  File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\cursors.py", line 308, in _query conn.query(q)
  File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\connections.py", line 820, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\connections.py", line 1002, in _read_query_result result.read()
  File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\connections.py", line 1285, in read first_packet = self.connection._read_packet()
  File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\connections.py", line 946, in _read_packet packet_header = self._read_bytes(4)
  File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\connections.py", line 982, in _read_bytes 2013, "Lost connection to MySQL server during query")
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2013, 'Lost connection to MySQL server during query') [SQL: 'SELECT apscheduler_jobs.id, apscheduler_jobs.job_state \nFROM apscheduler_jobs \nWHERE apscheduler_jobs.next_run_time <= %(next_run_time_1)s ORDER BY apscheduler_jobs.next_run_time'] [parameters: {'next_run_time_1': 1457445220.361246}]

有谁知道发生了什么事?以及如何解决它?

1 个答案:

答案 0 :(得分:0)

interactive_timeout的设置是什么?

<强> WAIT_TIMEOUT:

描述:服务器在关闭连接之前等待连接变为活动状态的时间(以秒为单位)。当线程从全局值启动时,如果连接是非交互式,则会初始化会话值,如果连接是交互式的,则从interactive_timeout值启动会话值。