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

标签 python python-3.x macos tkinter crash

我有一个 Python 程序,因为它太大了,这里是它的源代码的链接:
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;参见,例如 When the GIL is released?)的组件相结合的代码中。
根据您的描述,不清楚您的代码的哪一部分导致了错误(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)))
因此,该函数似乎是使用 undefined variable string 调用的。 .
我有这个 PyEval_RestoreThread由于各种原因而出现几次错误:
  • 当来自 cythonized Python 模块(在 Mac 上为 .so)的函数调用来自仅间接导入的另一个模块的函数时。例如,如果模块 a.py进口b.py , b.py进口c.py ,以及 a.py 中的函数使用来自 c.py 的函数.当从 PyCharm 或终端通过 python3 myprogram.py 运行时,没有错误,但是一旦模块被 cythonized,a.so 中的函数找不到来自 c.so 的函数了,我得到这个错误。
  • 在辅助线程中未捕获的异常;例如,当我没有检查从文件中读取的变量的类型,并试图在我的(PySimpleGUI = tkinter-based)GUI 中更新一个字符串时,它的值不是字符串。
  • 关于Python 应用程序因 "PyEval_RestoreThread: the function must be called with the GIL held, but the GIL is released"崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66795159/

    相关文章:

    python - Windows 缺少 Python.h

    python - 有没有办法解码包含字符串格式数字的 json?

    python - 我如何在 TensorFlow 中使用我自己的图像?

    swift - 来自 NSImage (Swift) 的像素信息

    macOS:在用户系统上安装 dylib 的正确位置是什么?

    ios - 如何使用和调试不存在模拟器的 iOS 版本?

    python 不同长度的日期并提取年份信息

    python-3.x - Tensorflow 对象检测 API 培训问题

    python - 使用 Python 从 Linux 中的不同文件创建 Excel 工作表

    python - findContours是否有理由给我一个错误,尽管它不应该这样吗?