并发读取MYSQL数据库出错

问题描述 投票:0回答:1

我有一个烧瓶程序。在'/'路由的get方法上,python代码通过以下方式访问MYSQL数据库

""" returns cash of current user """
def returncash(argdb):
    argdb.execute("SELECT cash FROM fin_users WHERE id = %s", (session['user_id'],))
    row = argdb.fetchall()

    return row[0]['cash']

这在大多数情况下都有效。但是,如果在我的浏览器中,我在“/”路线上快速重新加载页面两次,程序就会挂起。我已经将范围缩小到这条线

argdb.execute("SELECT cash FROM fin_users WHERE id = %s", (session['user_id'],))

似乎它试图同时访问相同的记录并冻结?没有错误消息,它只是永远不会到达下一行。如果我再点击刷新几次,我会收到以下错误

ERROR in app: Exception on / [GET]
Traceback (most recent call last):
  File "E:\myPrograms\financePlus\venv\Lib\site-packages\flask\app.py", line 1473, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\myPrograms\financePlus\venv\Lib\site-packages\flask\app.py", line 882, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\myPrograms\financePlus\venv\Lib\site-packages\flask\app.py", line 880, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\myPrograms\financePlus\venv\Lib\site-packages\flask\app.py", line 865, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\myPrograms\financePlus\landf.py", line 12, in innerFunction
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "E:\myPrograms\financePlus\app.py", line 31, in index
    cash = func.returncash(db)
           ^^^^^^^^^^^^^^^^^^^
  File "E:\myPrograms\financePlus\func.py", line 37, in returncash
    argdb.execute("SELECT cash FROM fin_users WHERE id = %s", (session['user_id'],))
  File "E:\myPrograms\financePlus\venv\Lib\site-packages\mysql\connector\cursor.py", line 551, in execute
    self._handle_result(self._connection.cmd_query(stmt))
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\myPrograms\financePlus\venv\Lib\site-packages\mysql\connector\connection.py", line 490, in cmd_query
    result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\myPrograms\financePlus\venv\Lib\site-packages\mysql\connector\connection.py", line 404, in _handle_result
    columns[i] = self._protocol.parse_column(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\myPrograms\financePlus\venv\Lib\site-packages\mysql\connector\protocol.py", line 239, in parse_column
    (packet, _) = utils.read_lc_string(packet)  # db
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\myPrograms\financePlus\venv\Lib\site-packages\mysql\connector\utils.py", line 199, in read_lc_string
    if buf[0] == 251:  # \xfb
       ~~~^^^
IndexError: bytearray index out of range

不太确定我能做些什么来解决这个问题。任何帮助表示赞赏!抱歉,我对编程还很陌生,希望这一切都有意义

我尝试在读取记录中添加锁定表,因为我认为这可能是导致问题的原因

""" returns cash of current user """
def returncash(argdb):

    argdb.execute("LOCK TABLES fin_users READ")
    argdb.execute("SELECT cash FROM fin_users WHERE id = %s", (session['user_id'],))
    row = argdb.fetchall()
    argdb.execute("UNLOCK TABLES")

    return row[0]['cash']

但是我收到以下错误

"GET / HTTP/1.1" 500 -
[2024-05-21 14:25:39,567] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
  File "E:\myPrograms\financePlus\venv\Lib\site-packages\flask\app.py", line 1473, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\myPrograms\financePlus\venv\Lib\site-packages\flask\app.py", line 882, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\myPrograms\financePlus\venv\Lib\site-packages\flask\app.py", line 880, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\myPrograms\financePlus\venv\Lib\site-packages\flask\app.py", line 865, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\myPrograms\financePlus\landf.py", line 12, in innerFunction
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "E:\myPrograms\financePlus\app.py", line 31, in index
    cash = func.returncash(db)
           ^^^^^^^^^^^^^^^^^^^
  File "E:\myPrograms\financePlus\func.py", line 37, in returncash
    argdb.execute("SELECT cash FROM fin_users WHERE id = %s", (session['user_id'],))
  File "E:\myPrograms\financePlus\venv\Lib\site-packages\mysql\connector\cursor.py", line 551, in execute
    self._handle_result(self._connection.cmd_query(stmt))
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\myPrograms\financePlus\venv\Lib\site-packages\mysql\connector\connection.py", line 490, in cmd_query
    result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\myPrograms\financePlus\venv\Lib\site-packages\mysql\connector\connection.py", line 384, in _handle_result
    elif packet[4] == 0:
         ~~~~~~^^^
IndexError: bytearray index out of range

======================编辑====================

按照@shadow的建议,我尝试使用

更新我的mysql.connector

python -m pip install mysql-connector-python

成功了。

然后再次运行程序,双击'/'路径处的页面刷新。这次程序没有冻结而是立即出现以下错误

Traceback (most recent call last):
  File "E:\myPrograms\financePlus\venv\Lib\site-packages\mysql\connector\connection_cext.py", line 697, in cmd_query
    self._cmysql.query(
_mysql_connector.MySQLInterfaceError: Lost connection to MySQL server during query

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

Traceback (most recent call last):
  File "E:\myPrograms\financePlus\venv\Lib\site-packages\flask\app.py", line 1473, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\myPrograms\financePlus\venv\Lib\site-packages\flask\app.py", line 882, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\myPrograms\financePlus\venv\Lib\site-packages\flask\app.py", line 880, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\myPrograms\financePlus\venv\Lib\site-packages\flask\app.py", line 865, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\myPrograms\financePlus\landf.py", line 12, in innerFunction
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "E:\myPrograms\financePlus\app.py", line 31, in index
    cash = func.returncash(db)
           ^^^^^^^^^^^^^^^^^^^
  File "E:\myPrograms\financePlus\func.py", line 35, in returncash
    argdb.execute("SELECT cash FROM fin_users WHERE id = %s", (session['user_id'],))
  File "E:\myPrograms\financePlus\venv\Lib\site-packages\mysql\connector\cursor_cext.py", line 372, in execute
    result = self._cnx.cmd_query(
             ^^^^^^^^^^^^^^^^^^^^
  File "E:\myPrograms\financePlus\venv\Lib\site-packages\mysql\connector\opentelemetry\context_propagation.py", line 102, in wrapper
    return method(cnx, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\myPrograms\financePlus\venv\Lib\site-packages\mysql\connector\connection_cext.py", line 705, in cmd_query
    raise get_mysql_exception(
mysql.connector.errors.OperationalError: 2013 (HY000): Lost connection to MySQL server during query
127.0.0.1 - - [22/May/2024 16:31:08] "GET / HTTP/1.1" 500 - ```


Will continue to try with another alternative connector
flask mysql-connector
1个回答
0
投票

如果在2024年遇到

if buf[0] == 251:  # \xfb
,请将mysql连接器更新到最新版本。专业提示:包名称应该是
mysql-connector-python
,出于某种原因,我使用了
mysql.connector

© www.soinside.com 2019 - 2024. All rights reserved.