Python 应用崩溃 "PyEval_RestoreThread: the function must be called with the GIL held, but the GIL is released"

Python app crashed with "PyEval_RestoreThread: the function must be called with the GIL held, but the GIL is released"

我有一个 Python 程序,因为它太大了,这里是它的源代码 link: Link

当我在 Mac 上 运行 时,有时我会得到这个奇怪的异常:

Fatal Python error: PyEval_RestoreThread: the function must be called with the GIL held, but the GIL is released (the current Python thread state is NULL)
Python runtime state: initialized

Current thread 0x0000000115f8ce00 (most recent call first):
  File "/usr/local/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 1429 in mainloop
  File ".../PyPlusSource/pyplus.py", line 1435 in <module>

那么为什么会这样呢?请用简单的话解释一下。感谢您的任何想法!

编辑:
我在 MacOS 11.2.3 Big Sur [不是测试版],安装了 Python 3.9.2 [不是测试版]。而且这个错误很难重现

一般来说,这只可能发生在将 Python 与其他语言的组件(通常是 C;参见,例如 )组合的代码中。

根据你的描述,不清楚你的代码是哪一部分导致了错误(11 天前问的原始问题说是 line 1435,你 6 天前添加的视频显示是 line 1452,并且您提到的 GitHub 处的 PyPlus 代码已在 2 天前更改,目前只有 1441 行),因此,您需要检查自己的代码中究竟是哪里产生了错误。

该错误进一步涉及 tkinter/__init__.py 中的第 1429 行(据我在 Python 3.9 版本的 tkinter 中所见)是 if string:以下功能:

def _getints(self, string):
    """Internal function."""
    if string:
        return tuple(map(self.tk.getint, self.tk.splitlist(string)))

所以,似乎函数是用未定义的变量调用的string

由于各种原因,我遇到过几次这个 PyEval_RestoreThread 错误:

  • 当来自 cythonized Python 模块(Mac 上的 .so)的函数调用来自导入的另一个模块的函数时 间接地。例如,如果模块 a.py 导入 b.pyb.py 导入 c.py,并且 a.py 中的函数使用 c.py 中的函数。当 运行 来自 PyCharm 或来自终端 python3 myprogram.py 时,没有错误,但是一旦模块被 cythonized,a.so 中的函数就无法找到来自 [= 的函数27=] 了,我得到了这个错误。

  • 在辅助线程中出现未捕获的异常;例如,当我没有检查从文件中读取的变量类型并试图更新我的(PySimpleGUI = tkinter-based)GUI 中的字符串时,它的值不是字符串。