我有一个 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
由于各种原因而出现几次错误:.so
)的函数调用来自仅间接导入的另一个模块的函数时。例如,如果模块 a.py
进口b.py
, b.py
进口c.py
,以及 a.py
中的函数使用来自 c.py
的函数.当从 PyCharm 或终端通过 python3 myprogram.py
运行时,没有错误,但是一旦模块被 cythonized,a.so
中的函数找不到来自 c.so
的函数了,我得到这个错误。关于Python 应用程序因 "PyEval_RestoreThread: the function must be called with the GIL held, but the GIL is released"崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66795159/