Python Flask 应用程序中的“无法在静态 TLS 块中分配内存”

Posted

技术标签:

【中文标题】Python Flask 应用程序中的“无法在静态 TLS 块中分配内存”【英文标题】:"cannot allocate memory in static TLS block" in a Python Flask application 【发布时间】:2020-11-28 03:00:18 【问题描述】:

我在网上搜索过,但没有找到任何解决我问题的方法。我正在使用 Python 在 Flask 中编写一个 Web 应用程序,但是在将我的数据传输到新计算机并重新创建虚拟环境后,我不再能够使用 Flask 的内置开发 Web 服务器。我在下面得到追溯。我不知道问题出在 SQL 模块还是 Flask 模块上,但我尝试从源代码重新安装和编译它们,但无济于事。即使不在虚拟环境中,应用程序也会抛出相同的错误。

127.0.0.1 - - [07/Aug/2020 14:29:49] "GET / HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/jhonig/ttx/venv/lib/python3.8/site-packages/sqlalchemy/util/_collections.py", line 1020, in __call__
    return self.registry[key]
KeyError: 139700690765568

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/jhonig/ttx/venv/lib/python3.8/site-packages/flask/app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/jhonig/ttx/venv/lib/python3.8/site-packages/flask/app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "/home/jhonig/ttx/venv/lib/python3.8/site-packages/flask/app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/jhonig/ttx/venv/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/jhonig/ttx/venv/lib/python3.8/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/jhonig/ttx/venv/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
    return self.finalize_request(rv)
  File "/home/jhonig/ttx/venv/lib/python3.8/site-packages/flask/app.py", line 1969, in finalize_request
    response = self.process_response(response)
  File "/home/jhonig/ttx/venv/lib/python3.8/site-packages/flask/app.py", line 2268, in process_response
    self.session_interface.save_session(self, ctx.session, response)
  File "/home/jhonig/ttx/venv/lib/python3.8/site-packages/flask_session/sessions.py", line 534, in save_session
    saved_session = self.sql_session_model.query.filter_by(
  File "/home/jhonig/ttx/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 519, in __get__
    return type.query_class(mapper, session=self.sa.session())
  File "/home/jhonig/ttx/venv/lib/python3.8/site-packages/sqlalchemy/orm/scoping.py", line 78, in __call__
    return self.registry()
  File "/home/jhonig/ttx/venv/lib/python3.8/site-packages/sqlalchemy/util/_collections.py", line 1022, in __call__
    return self.registry.setdefault(key, self.createfunc())
  File "/home/jhonig/ttx/venv/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 3286, in __call__
    return self.class_(**local_kw)
  File "/home/jhonig/ttx/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 138, in __init__
    bind = options.pop('bind', None) or db.engine
  File "/home/jhonig/ttx/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 937, in engine
    return self.get_engine()
  File "/home/jhonig/ttx/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 956, in get_engine
    return connector.get_engine()
  File "/home/jhonig/ttx/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 561, in get_engine
    self._engine = rv = self._sa.create_engine(sa_url, options)
  File "/home/jhonig/ttx/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 966, in create_engine
    return sqlalchemy.create_engine(sa_url, **engine_opts)
  File "/home/jhonig/ttx/venv/lib/python3.8/site-packages/sqlalchemy/engine/__init__.py", line 500, in create_engine
    return strategy.create(*args, **kwargs)
  File "/home/jhonig/ttx/venv/lib/python3.8/site-packages/sqlalchemy/engine/strategies.py", line 87, in create
    dbapi = dialect_cls.dbapi(**dbapi_args)
  File "/home/jhonig/ttx/venv/lib/python3.8/site-packages/sqlalchemy/dialects/mysql/mysqldb.py", line 118, in dbapi
    return __import__("MySQLdb")
  File "/home/jhonig/ttx/venv/lib/python3.8/site-packages/MySQLdb/__init__.py", line 18, in <module>
    from . import _mysql
ImportError: /lib/x86_64-linux-gnu/libstdc++.so.6: cannot allocate memory in static TLS block

【问题讨论】:

【参考方案1】:

对于可能遇到此问题的任何人,libmysqlclient21 软件包存在问题,该问题已在 8.0.21 版本中得到修复:

错误:https://bugs.launchpad.net/ubuntu/+source/opencv/+bug/1890170

修复:https://bugs.launchpad.net/ubuntu/+source/mysql-8.0/+bug/1889851

【讨论】:

【参考方案2】:

使用sudo apt upgrade 升级我的系统似乎可以解决问题,但我不知道如何解决。似乎某些 MySQL 软件包已更新,所以也许这是一个错误?

为了后代,这里是升级的软件包列表:

The following packages will be upgraded:
  libmysqlclient-dev libmysqlclient21 libmysqlclient21:i386 linux-generic
  linux-headers-5.4.0-7634 linux-headers-5.4.0-7634-generic linux-headers-generic
  linux-image-5.4.0-7634-generic linux-image-generic linux-libc-dev
  linux-modules-5.4.0-7634-generic linux-modules-extra-5.4.0-7634-generic
  linux-system76 openjdk-8-jre-headless pop-shell
15 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

【讨论】:

以上是关于Python Flask 应用程序中的“无法在静态 TLS 块中分配内存”的主要内容,如果未能解决你的问题,请参考以下文章

Python flask中的配置

Python-Flask中的render_template不起作用

Flask Python中的URL前缀使用Blueprint部署到Heroku

Google App Engine / Datastore / Flask / Python app 中的内存泄漏

Flask Python 中的 HTTP 状态 405 和 Restful API

python flask 基础入门