Python 应用程序因“PyEval_RestoreThread:必须在持有 GIL 的情况下调用该函数,但已释放 GIL"而崩溃 [英] Python app crashed with "PyEval_RestoreThread: the function must be called with the GIL held, but the GIL is released"
问题描述
我有一个 Python 程序,因为它太大了,这里是它的源代码链接:链接
I have a Python Program, and because it's too big, here's the link to its source code: Link
当我在 Mac 上运行它时,有时会出现这个奇怪的异常:
When I run it on Mac, sometimes I get this weird exception:
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>
那么为什么会发生这种情况呢?请用简单的话解释一下.感谢您的任何想法!
So why is this happenning? Please explain in simple words. Thanks for any ideas!
我在 MacOS 11.2.3 Big Sur [非测试版] 上,安装了 Python 3.9.2 [不再是测试版].而且这个错误很难重现
I'm on MacOS 11.2.3 Big Sur [Not beta], with Python 3.9.2 [Not beta again] installed. And this error is hard to reproduce
推荐答案
一般来说,这应该只发生在将 Python 与其他语言(通常是 C;参见,例如,GIL 何时发布?).
In general, this should only possibly happen in a code combining Python with components in other languages (typically C; see, e.g., When the GIL is released?).
根据您的描述,不清楚您的代码的哪一部分导致了错误(11 天前提出的原始问题说它是 line 1435
,您 6 天前添加的视频显示它是 line1452
,并且您引用的 GitHub 上的 PyPlus 代码在 2 天前已更改,目前只有 1441 行),因此,您需要检查自己代码中产生错误的确切位置.
From your description, it's not clear which part of your code causes the error (the original question asked 11 days ago says it's line 1435
, your video added 6 days ago shows it's line 1452
, and the PyPlus code at GitHub you refer to was changed 2 days ago and currently only has 1441 lines) and therefore, you'll need to check yourself where exactly in your code the error is produced.
该错误进一步指的是 tkinter/__init__.py
中的第 1429 行(据我在 Python 3.9 版本的 tkinter
中看到)是 if string:
在以下函数中:
The error further refers to line 1429 in tkinter/__init__.py
which (as far as I can see in my Python 3.9 version of tkinter
) is if string:
in the following function:
def _getints(self, string):
"""Internal function."""
if string:
return tuple(map(self.tk.getint, self.tk.splitlist(string)))
因此,该函数似乎是使用未定义的变量 string
调用的.
So, it seems that the function is called with undefined variable string
.
由于各种原因,我多次收到此 PyEval_RestoreThread
错误:
I have got this PyEval_RestoreThread
error a couple of times with various reasons:
当来自 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
并且我收到此错误.
When a function from a cythonized Python module (
.so
on Mac) calls a function from another module that was imported only indirectly. For example, if modulea.py
importsb.py
,b.py
importsc.py
, and a function ina.py
uses a function fromc.py
. When running from PyCharm or from terminal viapython3 myprogram.py
, there are no errors but as soon as the modules are cythonized, the functions ina.so
cannot found the functions fromc.so
anymore and I get this error.
在辅助线程中未捕获的异常;例如,当我没有检查从文件中读取的变量的类型并尝试使用非字符串值更新我的(PySimpleGUI = tkinter-based)GUI 中的字符串时.
On an uncaught exception in a secondary thread; for example when I haven't checked the type of a variable read from a file and was trying to update a string in my (PySimpleGUI = tkinter-based) GUI with its value that was no string.
这篇关于Python 应用程序因“PyEval_RestoreThread:必须在持有 GIL 的情况下调用该函数,但已释放 GIL"而崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!