gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:16:50 27 4
gpt4 key购买 nike

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

“查询期间失去与 MySQL 服务器的连接”

为了找到答案,我尝试对其进行一些测试,并发现如果我的数据库(MySQL)“wait_timeout”小于计划间隔时间,则会发生此错误。(抱歉,我犯了一些错误...小于...)

在测试中:

  • 我的工作设置

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

  • 我的数据库设置

    等待超时= 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}]

有谁知道这是怎么回事吗?以及如何修复它?

最佳答案

interactive_timeout 的设置是什么?

等待超时:

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

关于python - apscheduler 在查询期间丢失与 MySQL 服务器的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35872240/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com